Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > lower case to upper case

Reply
Thread Tools

lower case to upper case

 
 
Janice
Guest
Posts: n/a
 
      12-10-2004
char* line = "abcd";
How to convert the line to upper case and print?
Any option for printf to do this?
Thanx


 
Reply With Quote
 
 
 
 
Ravi Uday
Guest
Posts: n/a
 
      12-10-2004


Janice wrote:
> char* line = "abcd";
> How to convert the line to upper case and print?
> Any option for printf to do this?
> Thanx
>
>


Not sure if there is a exclusive function in 'C' to do it
but tivially you could do:

- Get each character one by one from the input array/string.
- Check whether the character is lower/upper using 'islower'or 'isupper'
functions
- If character is upper case, then Add 32 to get its lower case
equivalent or
- If character is lower case Subtract 32 to get its upper case.
- Store or print the converted characters.

You might want to do error checking on the input array too if they are
valid alphabets !

- Ravi

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      12-10-2004
Ravi Uday <(E-Mail Removed)> scribbled the following:
> Janice wrote:
>> char* line = "abcd";
>> How to convert the line to upper case and print?
>> Any option for printf to do this?
>> Thanx


> Not sure if there is a exclusive function in 'C' to do it


There is. Check out toupper().

> but tivially you could do:


> - Get each character one by one from the input array/string.
> - Check whether the character is lower/upper using 'islower'or 'isupper'
> functions
> - If character is upper case, then Add 32 to get its lower case
> equivalent or
> - If character is lower case Subtract 32 to get its upper case.
> - Store or print the converted characters.


Bzzzt. No one told you you were using ASCII, ISO-8859-1 or any other
charset where upper and lower case are 32 bytes apart.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
 
Reply With Quote
 
Zoran Cutura
Guest
Posts: n/a
 
      12-10-2004
Ravi Uday <(E-Mail Removed)> wrote:
>
>
> Janice wrote:
>> char* line = "abcd";
>> How to convert the line to upper case and print?
>> Any option for printf to do this?
>> Thanx
>>
>>

>
> Not sure if there is a exclusive function in 'C' to do it
> but tivially you could do:
>
> - Get each character one by one from the input array/string.
> - Check whether the character is lower/upper using 'islower'or 'isupper'
> functions
> - If character is upper case, then Add 32 to get its lower case
> equivalent or
> - If character is lower case Subtract 32 to get its upper case.
> - Store or print the converted characters.
>
> You might want to do error checking on the input array too if they are
> valid alphabets !
>



ever heard of tolower/toupper?

Your suggestions are not portable and will only function on a machine
where all lower case characters are represented by a value that is by 32
bigger than the according upper case characters representation. As this
need not actually be the case the standard provides you with functions
that need to be supported by your implementations, which makes usage of
them portable.

--
Z ((E-Mail Removed))
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
 
Reply With Quote
 
Ravi Uday
Guest
Posts: n/a
 
      12-10-2004


Joona I Palaste wrote:
> Ravi Uday <(E-Mail Removed)> scribbled the following:
>
>>Janice wrote:
>>
>>>char* line = "abcd";
>>>How to convert the line to upper case and print?
>>>Any option for printf to do this?
>>>Thanx

>>

>
>>Not sure if there is a exclusive function in 'C' to do it

>
>
> There is. Check out toupper().

Yes it is, missed it some how, Thanks.
>
>
>>but tivially you could do:

>
>
>>- Get each character one by one from the input array/string.
>>- Check whether the character is lower/upper using 'islower'or 'isupper'
>> functions
>>- If character is upper case, then Add 32 to get its lower case
>> equivalent or
>>- If character is lower case Subtract 32 to get its upper case.
>>- Store or print the converted characters.


To O.P.: the above works for ASCII character set only
>
>
> Bzzzt. No one told you you were using ASCII, ISO-8859-1 or any other
> charset where upper and lower case are 32 bytes apart.
>


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-10-2004
Janice wrote:
>
> char* line = "abcd";
> How to convert the line to upper case and print?
> Any option for printf to do this?


You can't and no. However if you had defined line as:

char line[] = "abcd";

you would have been able to convert it. Now you should spend some
time thinking about what the difference is. It is fundamental.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      12-10-2004
"Janice" <(E-Mail Removed)> wrote in news:cpbs8q$(E-Mail Removed):

> char* line = "abcd";


Pointer mis-match, char *line should not point to a const char * "abcd".
What if "abcd" is placed into non-writable memory?

> How to convert the line to upper case and print?


Use a writable array, like CBFalconer suggests, then call toupper() on the
array.

> Any option for printf to do this?


printf("%s\n", name-of-writable-array);

> Thanx

Thanks

--
- Mark ->
--
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-10-2004
Mark A. Odell wrote:
> "Janice" <(E-Mail Removed)> wrote in news:cpbs8q$(E-Mail Removed):
>
>>char* line = "abcd";

>
> Pointer mis-match, char *line should not point to a const char * "abcd".
> What if "abcd" is placed into non-writable memory?


No mismatch. The literal creates an array of ordinary
`char', not `const'-qualified. True, that array cannot be
written safely, but its type is non-`const' anyhow.

>>How to convert the line to upper case and print?

>
> Use a writable array, like CBFalconer suggests, then call toupper() on the
> array.


The argument to toupper() is an `int', not an array.
And there is no need for a writeable array anyhow:

char *line = "abcd";
while (*line != '\0')
putchar (toupper( (unsigned char)*line++ ));

(The `(unsigned char)' cast guards against the possibility
that negative-valued characters might appear in the string.
In this particular example all the characters have positive
values, but get in the habit of using the cast anyhow.)

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      12-10-2004
Eric Sosman <(E-Mail Removed)> wrote in
news:cpcfdq$s0k$(E-Mail Removed):

>>>char* line = "abcd";

>>
>> Pointer mis-match, char *line should not point to a const char *
>> "abcd". What if "abcd" is placed into non-writable memory?

>
> No mismatch. The literal creates an array of ordinary
> `char', not `const'-qualified. True, that array cannot be
> written safely, but its type is non-`const' anyhow.


You're rigth, sadly. What an awful choice. I still find bugs like this
where people assume a char * is writable (as one might expect) but where
the pointer points to a string literal. Wouldn't it have been better to
have string literals be of type const char *?

--
- Mark ->
--
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-10-2004
CBFalconer <(E-Mail Removed)> writes:
> Janice wrote:
>>
>> char* line = "abcd";
>> How to convert the line to upper case and print?
>> Any option for printf to do this?

>
> You can't and no. However if you had defined line as:
>
> char line[] = "abcd";
>
> you would have been able to convert it. Now you should spend some
> time thinking about what the difference is. It is fundamental.


That depends on whether "convert" means to convert it in-place, or to
create a converted copy. The problem statement doesn't make this
clear.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Emacs, VHDL Mode - Upper Case Enum Values & Upper Case Constants (not working)? BlackHelicopter VHDL 0 01-31-2013 04:12 AM
Regular Expression to Replace UPPER Case Text with lower case text penny Perl Misc 28 03-10-2008 01:14 AM
Upper and lower case Ch. Rajinder Nijjhar Jatt Computer Support 6 12-26-2004 09:20 PM
Regular expression weirdness with upper and lower case semovrs@concord.edu Perl 4 12-07-2004 01:35 PM
Upper and Lower case letters Ch. Rajinder Nijjhar Jatt Computer Support 5 11-02-2003 05:16 PM



Advertisments