My question is very brief, it is how I could convert a letter to uppercase of a character, in my case a char can be converted to lowercase. Ex:
char key[16];
key = "A"
How could I do the conversion so that my char was lowercase. I have tried using tolower but it has to be an integer, ex:
key = tolower(*key);
In my case it returns the following error:
incompatible types in assignment of 'int' to 'char [16]'|
However, the answer is very long. Let's start!
Short answer.
You can use
std::tolower
like this:Long answer.
We have two versions of
std::tolower
:In the header
<cctype>
:In the header
<locale>
:The header version
<cctype>
belongs to the C libraries while the header version<locale>
belongs to the C++ libraries. If you are programming in C++ you should use<locale>
.In addition to what was said in the previous paragraph, why is the version better
<locale>
than the version of<cctype>
?:char
) to integer (receives aint
) and returns an integer (returnsint
) that will have to be converted back to character (char
).charT
) and converts it to lowercase using the language locale; so not only does it take into account all character types (char
,wchar_t
,char16_t
andchar32_t
) without doing data transformations, it also takes into account the language they are written in!So to lowercase a character using the configured language setting you would do:
Other things to keep in mind.
Doing this is terrible!
You request 16 characters and then you re-assign the pointer, that is not allowed in C++ and that is why you get the error
incompatible types in assignment of 'int' to 'char [16]'
, to create onekey
that contains"A"
you must do this:Considering your question -> Lowercase a char using C++, not going into details already mentioned by eferion for example and shown by PaperBirdMaster in his type error question
is not assignable
, you can use the difference that exists in the ASCII tablein between the characters in uppercase and the same but in lowercase , the lowercase are, so to speak, 32 positions away from the same character in uppercase, knowing this you can do something like the following (simple example on a char) :
stdout:
TestIdeone
PS: To put a lowercase letter in uppercase, you would only have to use
-= 32
for example instead of+= 32
.