Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Toupper and tolower

25 views
Skip to first unread message

Bob Zimmerman

unread,
Feb 1, 2002, 1:10:10 AM2/1/02
to
Hello
Can someone show me an example of using toupper and tolower? I
can't seem to find anything about it in my book. Thanks

Dann Corbit

unread,
Feb 1, 2002, 1:55:58 AM2/1/02
to
"Bob Zimmerman" <knightm...@hotmail.com> wrote in message
news:ceeda4cd.02013...@posting.google.com...

> Hello
> Can someone show me an example of using toupper and tolower? I
> can't seem to find anything about it in my book. Thanks

Buy a better book [FCOL].

int toupper( int c );
int tolower( int c );

int capitalized_letter = toupper(some_character_upper_or_lower);
int lowercase_letter = tolower(some_character_upper_or_lower);
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm

Erik de Castro Lopo

unread,
Feb 1, 2002, 2:23:48 AM2/1/02
to

char ch ;

ch = toupper ('a') ;
putchar (ch) ;

ch = tolower ('A') ;
putchar (ch) ;


Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo nos...@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"Using Java as a general purpose application development language
is like going big game hunting armed with Nerf weapons."
-- Author Unknown

Simon Biber

unread,
Feb 1, 2002, 2:43:28 AM2/1/02
to

Here's an example:

#include <stdio.h>
#include <ctype.h>

#define BUFSIZE 128

int main(void)
{
char buf[BUFSIZE]={0}, *p;

printf("Enter a line of text: ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);

for(p=buf; *p != 0; p++)
{
putchar(tolower((unsigned char)*p));
}
for(p=buf; *p != 0; p++)
{
putchar(toupper((unsigned char)*p));
}
return 0;
}

And here's a sample run:
Enter a line of text: Hello, World!
hello, world!
HELLO, WORLD!

Note:
- The prototype of main is int main(void), which is
preferred to int main() or main() or the plain wrong
void main().

- The usage of fgets, rather than gets. This is because
gets might overflow the array, and write into other
memory, which is a security risk. Also fgets leaves
the newline character in the buffer, and that's
desireable in this example.

- The idiom for iterating through a string with a
pointer rather than using array indexing. On some
unoptimised compilers, pointers are faster. I also
simply prefer it in a clear case like this.

- The use of a cast to unsigned char for each char
value. This is because if char is signed and the
user entered some extended-characters which would
be negative, that would have undefined behaviour
when passed to the toupper or tolower functions.

--
Simon.


Tor Rustad

unread,
Feb 1, 2002, 4:32:42 PM2/1/02
to
"Erik de Castro Lopo" <nos...@mega-nerd.com> wrote in message
> Bob Zimmerman wrote:
> >
> > Hello
> > Can someone show me an example of using toupper and tolower? I
> > can't seem to find anything about it in my book. Thanks
>
> char ch ;
>
> ch = toupper ('a') ;
> putchar (ch) ;

How hard is it to read a C book these days?

FYI, toupper() returns int.


--
Tor <torust AT online DOT no>

Ben Pfaff

unread,
Feb 1, 2002, 4:35:12 PM2/1/02
to
"Tor Rustad" <tor...@online.no.spam> writes:

> "Erik de Castro Lopo" <nos...@mega-nerd.com> wrote in message

> > char ch ;
> >
> > ch = toupper ('a') ;
> > putchar (ch) ;
>
> How hard is it to read a C book these days?
>
> FYI, toupper() returns int.

Yeah--an `int' that will be a character unless you pass it EOF as
input. There's no good reason, in this case, not to assign the
result of toupper() to a `char'.
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan

Dann Corbit

unread,
Feb 1, 2002, 7:22:41 PM2/1/02
to
"Ben Pfaff" <b...@cs.stanford.edu> wrote in message
news:87vgdg3...@pfaff.stanford.edu...

> "Tor Rustad" <tor...@online.no.spam> writes:
>
> > "Erik de Castro Lopo" <nos...@mega-nerd.com> wrote in message
> > > char ch ;
> > >
> > > ch = toupper ('a') ;
> > > putchar (ch) ;
> >
> > How hard is it to read a C book these days?
> >
> > FYI, toupper() returns int.
>
> Yeah--an `int' that will be a character unless you pass it EOF as
> input. There's no good reason, in this case, not to assign the
> result of toupper() to a `char'.


Actually toupper()/tolower() have no method to signal out of bounds data
error. It's just like atof() and that ilk.

Mark McIntyre

unread,
Feb 1, 2002, 8:15:12 PM2/1/02
to
On Fri, 1 Feb 2002 16:22:41 -0800, "Dann Corbit" <dco...@connx.com>
wrote:

>"Ben Pfaff" <b...@cs.stanford.edu> wrote in message
>news:87vgdg3...@pfaff.stanford.edu...
>> "Tor Rustad" <tor...@online.no.spam> writes:
>>
>> > "Erik de Castro Lopo" <nos...@mega-nerd.com> wrote in message
>> > > char ch ;
>> > >
>> > > ch = toupper ('a') ;
>> > > putchar (ch) ;
>> >
>> > How hard is it to read a C book these days?
>> >
>> > FYI, toupper() returns int.
>>
>> Yeah--an `int' that will be a character unless you pass it EOF as
>> input. There's no good reason, in this case, not to assign the
>> result of toupper() to a `char'.

Ben, you're assuming the input data is in the range of a char.
Admittedly 'a' is in that range (although its an int, and you're
discarding bits). But in general, is that true? And should we be
giving advice thats highly specific to a code sample, without the
appropriate caveats.

>Actually toupper()/tolower() have no method to signal out of bounds data
>error. It's just like atof() and that ilk.

Bens' point was that 'a' is not able to generate such an error

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

Lew Pitcher

unread,
Feb 1, 2002, 8:21:14 PM2/1/02
to

#include <ctype.h>
#include <stdio.h>
{
char buf[] = "aAbBc";
char *pbuf;

printf("Before: buf[] = \"%s\"\n",buf);

for (pbuf = &buf[0]; *pbuf; ++pbuf)
if (islower(*pbuf)) *pbuf = toupper(*pbuf);
printf("After toupper(): buf[] = \"%s\"\n",buf);


for (pbuf = &buf[0]; *pbuf; ++pbuf)
if (isupper(*pbuf)) *pbuf = tolower(*pbuf);
printf("After tolower(): buf[] = \"%s\"\n",buf);
}


--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered (Slackware) Linux User #112576 (http://counter.li.org/)

Lawrence Kirby

unread,
Feb 2, 2002, 10:53:33 AM2/2/02
to
On Friday, in article <3C5B3F0A...@sympatico.ca>
lpit...@sympatico.ca "Lew Pitcher" wrote:

>Bob Zimmerman wrote:
>>
>> Hello
>> Can someone show me an example of using toupper and tolower? I
>> can't seem to find anything about it in my book. Thanks
>
> #include <ctype.h>
> #include <stdio.h>

One more line could have made this a complete program!

int main(void)

> {
> char buf[] = "aAbBc";
> char *pbuf;
>
> printf("Before: buf[] = \"%s\"\n",buf);
>
> for (pbuf = &buf[0]; *pbuf; ++pbuf)
> if (islower(*pbuf)) *pbuf = toupper(*pbuf);

You can simplify this to

*pbuf = toupper(*pbuf);

although this is safer

*pbuf = toupper((unsigned char)*pbuf);


> printf("After toupper(): buf[] = \"%s\"\n",buf);
>
>
> for (pbuf = &buf[0]; *pbuf; ++pbuf)
> if (isupper(*pbuf)) *pbuf = tolower(*pbuf);

*pbuf = tolower((unsigned char)*pbuf);

> printf("After tolower(): buf[] = \"%s\"\n",buf);
> }

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------

Lawrence Kirby

unread,
Feb 2, 2002, 10:41:48 AM2/2/02
to
On Saturday, in article
<t9fm5uo73p5brtac0...@4ax.com>
markmc...@spamcop.net "Mark McIntyre" wrote:

>On Fri, 1 Feb 2002 16:22:41 -0800, "Dann Corbit" <dco...@connx.com>
>wrote:
>
>>"Ben Pfaff" <b...@cs.stanford.edu> wrote in message
>>news:87vgdg3...@pfaff.stanford.edu...
>>> "Tor Rustad" <tor...@online.no.spam> writes:
>>>
>>> > "Erik de Castro Lopo" <nos...@mega-nerd.com> wrote in message
>>> > > char ch ;
>>> > >
>>> > > ch = toupper ('a') ;
>>> > > putchar (ch) ;
>>> >
>>> > How hard is it to read a C book these days?
>>> >
>>> > FYI, toupper() returns int.
>>>
>>> Yeah--an `int' that will be a character unless you pass it EOF as
>>> input. There's no good reason, in this case, not to assign the
>>> result of toupper() to a `char'.
>
>Ben, you're assuming the input data is in the range of a char.

The input data had better be in the range of an unsigned char.

>Admittedly 'a' is in that range (although its an int, and you're
>discarding bits). But in general, is that true? And should we be
>giving advice thats highly specific to a code sample, without the
>appropriate caveats.

It might be appropriate to define ch as unsigned char. If EOF is not
a valid inpout value then there is no need to define ch as int here.
The fact is that an awful lot of code assumes that value that represents
a valid character can be converted without problem to char. For example

char buffer[SIZE];
int i, ch;

while (i < SIZE && (ch = getchar()) != EOF && ch != '\n')
buffer[i++] = ch;

ch has type int because it needs to be able to handle EOF. Once that
is dealt with the value is stored in one of the char elements of buffer.
If you can accept this as OK in practice then you can't really complain
about making the loop body

buffer[i++] = toupper(ch);

i.e. converting the result of toupper() to char.

>>Actually toupper()/tolower() have no method to signal out of bounds data
>>error. It's just like atof() and that ilk.
>
>Bens' point was that 'a' is not able to generate such an error

If you pass out of bounds data to toupper() i.e. a value that cannot
be represented as an unsigned char and is not the value of EOF then
you have undefined behaviour. What you do or do not do with any
return value is then of no consequence.

Tor Rustad

unread,
Feb 2, 2002, 3:06:16 PM2/2/02
to
"Lawrence Kirby" <fr...@genesis.demon.co.uk> wrote in message

> On Saturday, in article
> <t9fm5uo73p5brtac0...@4ax.com>
> markmc...@spamcop.net "Mark McIntyre" wrote:
>
> >On Fri, 1 Feb 2002 16:22:41 -0800, "Dann Corbit" <dco...@connx.com>
> >wrote:
> >
> >>"Ben Pfaff" <b...@cs.stanford.edu> wrote in message
> >>news:87vgdg3...@pfaff.stanford.edu...
> >>> "Tor Rustad" <tor...@online.no.spam> writes:
> >>>
> >>> > "Erik de Castro Lopo" <nos...@mega-nerd.com> wrote in message
> >>> > > char ch ;
> >>> > >
> >>> > > ch = toupper ('a') ;
> >>> > > putchar (ch) ;
> >>> >
> >>> > How hard is it to read a C book these days?
> >>> >
> >>> > FYI, toupper() returns int.
> >>>
> >>> Yeah--an `int' that will be a character unless you pass it EOF as
> >>> input. There's no good reason, in this case, not to assign the
> >>> result of toupper() to a `char'.

This case isn't interesting, just use "putchar ('A')". ;-)

> >Ben, you're assuming the input data is in the range of a char.
>
> The input data had better be in the range of an unsigned char.

Input can be 'unsigned char' or EOF, ditto for the output.

That is the reason 'int' should be used, and not 'char'.

0 new messages