3 ways to uppercase
Author Message
3 ways to uppercase

I need to know three ways to change a character letter, if it is lowercase,
to an uppercase letter.

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

I need to know three ways to change a character letter, if it is lowercase,
to an uppercase letter.

--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

Quote:
> I need to know three ways to change a character letter, if it is
lowercase,
> to an uppercase letter.

There are indeed three ways to change a lower case character to upper case.

Given that you have these two lines:

#include <ctype.h>

and

char ch = 'a';

in appropriate places in your code, you can choose any one of these
techniques:

1) ch = (char)toupper((int)ch);
2) ch = (char)toupper((int)ch);
3) ch = (char)toupper((int)ch);

IIRC there is no other technique that is guaranteed to be portable across
different character sets. Therefore, EITHER I'm wrong and I missed a
couple, OR your lecturer, a guy who ought to know better, is convinced
and/or is trying to convince you that C is only implemented on ASCII-based
platforms.

--
Richard Heathfield

The bug stops here.

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase
Assuming that you're not dealing with UNICODE value.

Method 1:
Use ANSI-C function "toupper":

for( i = 0; str[i] != '\0'; i++ )
str[i] = toupper( str[i] );

Method 2:
Check the decimal value of the characters.  If it's between 97 and 122,
substract 32:

for( i = 0; str[i] != '\0'; i++ )
{
if( 122 >= str[i] >= 96 )
str[i] -= 32;
}

Method 3:
Build a table containing the upper case equivalence of each lower case
characters:

static charTable[2][27] = { "abcdefghijklmnopqrstuvwxyz",

"ABCDEFGHIJKLMNOPQRSTUVWXYZ" };

for( int i = 0; str[i] != '\0'; i++ )
{
for( int j = 0; charTable[j] != '\0'; j++ )

if( str[i] == charTable[0][j] )
{
str[i] = charTable[1][j];
break;
}
}
}

Hope this will help.

Nicolas Thomassin
CGI Consulting inc.

Quote:
> I need to know three ways to change a character letter, if it is
lowercase,
> to an uppercase letter.

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

Quote:

> I need to know three ways to change a character letter, if it is lowercase,
> to an uppercase letter.

1. Use toupper()

2. Use a data structure like struct {char lc, char uc} in a 26-element
array. Load each lowercase and its corresponding uppercase character
into the data structure, and create a search function.

3. Put all 26 letters into a relational database table. Use the uppercase
facility provided by the database to do the conversion when you select
the character.

Scott

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

Quote:

> > I need to know three ways to change a character letter, if it is
> lowercase,
> > to an uppercase letter.

> Assuming that you're not dealing with UNICODE value.

> Method 1:
>     Use ANSI-C function "toupper":

>     for( i = 0; str[i] != '\0'; i++ )
>         str[i] = toupper( str[i] );

Tick!

Quote:

> Method 2:
>     Check the decimal value of the characters.  If it's between 97 and
122,
> substract 32:

>     for( i = 0; str[i] != '\0'; i++ )
>     {
>         if( 122 >= str[i] >= 96 )
>             str[i] -= 32;
>     }

Bzzt! Assumes a particular character set.

Quote:

> Method 3:
>     Build a table containing the upper case equivalence of each lower
case
> characters:

Tick! I missed this possibility.

Two out of three ain't bad - but never forget that EBCDIC will bite you if
you turn your back on it.

--
Richard Heathfield

The bug stops here.

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

Quote:

> [snip]
> > > Method 3:
> > >     Build a table containing the upper case equivalence of each lower
> > case
> > > characters:

> > Tick! I missed this possibility.
> What will he use to build the table?

Whoops - I snipped his code. He posted something like:

char a[][27] = {"abcdefghijklmnopqrstuvwxyz", "ABCDEFGblahblah

and some code for looping through a[0] looking for a match, then using
a[1][i], which seemed a workable method to me.

Check the thread history or DejaNews for his exact code.

--
Richard Heathfield

The bug stops here.

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

Quote:

> Assuming that you're not dealing with UNICODE value.

Or any of a large number of other legal encodings.

Quote:
> Method 2:
>     Check the decimal value of the characters.  If it's between 97 and 122,
> substract 32:

>     for( i = 0; str[i] != '\0'; i++ )
>     {
>         if( 122 >= str[i] >= 96 )
>             str[i] -= 32;
>     }

if (122 >= str[1] >= 96)
is legal, but will not do what you want.  The check you want is
if (122 >= str[1] && str[1] >= 97) /* not 96 */
You may think that this is equivalent to
if ('z' >= str[i] && str[i] >='a')
but it is not.  On an EBCDIC implementation, it is the same as
if (':' >= str[1] && str[1] >='/')
and
str[1] -= 32;
which is str[1] -= ' '; in ASCII, is just wrong on an EBCDIC machine,
where 'a' == 129 and 'A' == 193, so 'a' - 'A' == -64.  Not only the value, but
the sign is wrong (note that ' '==64 in EBCDIC, so
abs('a'-'A') == ' ' in both).
Further, 'a' (129) through 'z' (169), includes "~{}\\" as well as 10 other codes
which you will not want to map.

Quote:
> Hope this will help.

I doubt it. Incorrect information is never a help unless clearly marked as
"Don't do this wrong thing."  Your implementation does not define the world.

--

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

Here's a trick I just read about (ch is some lowercase letter):

ch = (ch - 'a') + 'A';

Note 1:  It would seem this assumes an ASCII character set, which from

Note 2:  Why anyone would do this when toupper() is available is beyond me
(reinventing the wheel?)

Of course my perspective is still pretty limited...

Chris

Mon, 15 Oct 2001 03:00:00 GMT
3 ways to uppercase

Quote:
> Suggestion:
> Run from 0 to UCHAR_MAX through toupper() and also his function, then
tell
> me if they are identical.  Change code pages and do it again.  Repeat
until
> you run out of code pages.

The C programming language does not, as far as I am aware, define 'code
pages', whatever /they/ are.

Quote:
> All the world is an English-speaking ASCII-based VAX.  Or so I'm told.

ASCII-based? Regrettably, no. I have to deal with this reality every
working day.
VAX? No. Never used one, never seen one. Never even seen a photograph of
one.
English-speaking? Ah, well! MY world is English-speaking. YWMV.

No doubt my attitude would be different if I had to write apps for an
international user-base.

As it happens, I would use toupper( ) anyway, so I hope I can escape from
this with a little light grilling rather than a major flame.

/me crosses fingers, dons fireproof underwear and Dann-proof helmet, and
runs for cover. :-)

--
Richard Heathfield

The bug stops here.

Tue, 16 Oct 2001 03:00:00 GMT
3 ways to uppercase
On 29 Apr 1999 21:25:20 +0100, "Richard Heathfield"

Quote:

>> Method 2:
>>     Check the decimal value of the characters.  If it's between 97 and
>122,
>> substract 32:

>>     for( i = 0; str[i] != '\0'; i++ )
>>     {
>>         if( 122 >= str[i] >= 96 )
>>             str[i] -= 32;
>>     }

>Bzzt! Assumes a particular character set.

Not only that, it assumes a character set in which there are 27
letters.
--
Michael M Rubenstein

Tue, 16 Oct 2001 03:00:00 GMT

 Page 1 of 2 [ 25 post ] Go to page: [1] [2]

Relevant Pages