Finlandia_C Finlandia_C - 1 year ago 67
C Question

Capitalizing vowels, lower casing consonants

I have some code I have written which tries to capitalizes vowels and makes consonants lower case. However it only outputs everything as capital, i.e


is the output from


int main(void)
int a;
printf("Non vowels in lower cap: ");



int vowerls (int f)
f = getchar();
f = ? : putchar(tolower(cf));
return c;


What am I missing here?

Answer Source

The line

c = ( 'a' || 'e' || 'i' || 'o' || 'u') ? putchar(toupper(c)) : putchar(tolower(c));

is causing the problem. First, as @RichTolley said, you need to use == instead of =. Assuming you do that, what you would have is

c == ('a' || 'e' || 'i' || 'o' || 'u') : // etc.

Since parenthesis are first, it will start by evaluating the things in the parentheses and only after that will it compare with c. Since non-zero ints (and, by extension, chars) are truthy, it will see 'a', which it reads as 97, evaluate that as true, notice that there is an || after it, and stop evaluating, since true || anything is true. Therefore, it will always evaluate the first part of the ternary operator as true and print out a capital letter. What you need is

(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') : // etc.

This will actually check whether c is equal to one of those.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download