Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm hoping to find a way to change any lowercase letter to an uppercase no matter which letter is used. I saw online something about using the asciii values to accomplish this however I'm not sure how this would be applied to every letter.


This I believe will convert lowercase b to uppercase B, but how could I change this
to apply to any letter?
‘B’ = ‘b’ – ‘a’ + ‘A’

I greatly appreciate any advice or help you may offer.
Posted
Comments
Philippe Mori 11-Oct-13 20:51pm    
You should avoid doing that as it only works for characters between a and z (when using ASCII code page). It won't works for accentued characters and it won't follows current language rule when rule differes from language to language.

In any real life application, you want to use functions that take into account code page (437, 1252, Unicode), encoding (like UTF-8), selected language rules (English, French, Spanish...). Same thing wouls apply to sorting. Rules are diffeents depending on the language.

The safe way is to do something like:
C++
char c = ...;
if (islower(c)) c=toupper(c);

or
C++
wchar_t c = ...;
if (iswlower(c)) c=towupper(c);

See also http://www.cplusplus.com/reference/cctype/islower/[^] for char based text, or http://www.cplusplus.com/reference/cwctype/iswlower/[^] for wchar_t based text.
Cheers
Andi
 
Share this answer
 
v2
Comments
Philippe Mori 11-Oct-13 20:45pm    
No really necessary to check if it is a lowercase character first as it might almost double execution time.
Andreas Gieriet 11-Oct-13 23:06pm    
Yes, you are right. The toupper/tolower and its siblings already check for the appropriate range.
Cheers
Andi
The difference between the ASCII value for A (65) and a (97) is 32. This is the same for all letters. You can simply subtract 32 from the lower case value and that would be the upper case equivalent.
 
Share this answer
 
Comments
CAS1224 10-Oct-13 11:59am    
Thanks for the explanation it helps a lot.
You can use these functions[^] or these[^]. Or simply subtract hex 20 (decimal 32) like:
C++
char lowerChar = 'b';
char upperChar = lowerChar - 0x20;

NB you should check first that the character is lower case as described in the documentation links above.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900