fais fais - 1 year ago 71
C Question

toupper function in C with for loop

I don't understand why the following code doesn't work as I wanted it to.

int main()
char sentence[] = "this will be capitalised";
int i;

for (i=0; i<strlen(sentence); i++)


return (0);

I end up with this:

this will be capitalised

Program ended with exit code: 0

Thank you

Answer Source

Replace the line:


with this one:

   sentence[i] = toupper(sentence[i]);

The problem with your code is that toupper does not do the conversion in place, instead it returns the converted letter as its return value.

If you don't want to modify the string and you are only want to print it in uppercase form, then use WolfieeifloW's solution.

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