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 using tolower
-- it converts upper to lower case, and leaves other characters unchanged. You probably want something like:
for (i=0; foo[i]; i++)
foo[i] = tolower((unsigned char)foo[i]);
Note that when you call tolower
(and toupper
, isalpha
, etc.) you really need to cast your input to unsigned 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);
, assuming SIZE
== 20 (i.e., you want to specify one less than the size. Alternatively, you could use fgets
, like fgets(foo, 20, infile);
. Note that with fgets
, you specify the size of the buffer, not one less like you do with scanf
(and company like fscanf).
isalpha
to remove the non-alpha characters. – Compressionisupper
andtolower
applies only to a single char, not string. – Pashtofor
to iterate through every char of the string, check out #2662266 – Pashto