This question already has an answer here:
I have a char foo[SIZE]; //(string)
and have inputed it correctly using %s
(as in it printfs
the correct input), but now want to set it to lowercase. So I tried using
if (isupper(*foo))
*foo=tolower(*foo);
ie when I do:
printf("%s" foo); //I get the same text with upper case
The text does not seem to change. Thank you.
*foo=tolower(*foo); //doing *(foo+i) or foo[i] does not work either
because all of those options do not make sense
You should use it like this:
Try this
foo
isn't a pointer, so you don't want to use it as one. You also don't have to check whether a character is an upper-case letter before usingtolower
-- it converts upper to lower case, and leaves other characters unchanged. You probably want something like:Note that when you call
tolower
(andtoupper
,isalpha
, etc.) you really need to cast your input tounsigned char
. Otherwise, many (most?) characters outside the basic English/ASCII character set will frequently lead to undefined behavior (e.g., in a typical case, most accented characters will show up as negative numbers).As an aside, when you're reading the string, you don't want to use
scanf
with%s
-- you always want to specify the string length, something like:scanf("%19s", foo);
, assumingSIZE
== 20 (i.e., you want to specify one less than the size. Alternatively, you could usefgets
, likefgets(foo, 20, infile);
. Note that withfgets
, you specify the size of the buffer, not one less like you do withscanf
(and company like fscanf).