But the problem is, after a space it doesn't convert to uppercase anymore, I've tried some logic to fix it, (thats why the if statement is there) but it doesn't work
I got this from the textbook I'm reading. it has the same problem too.
Code:
#include<stdio.h> main() { char str[80]; int i,delt='a'-'A'; printf("Enter a string less than 80 characters:\n"); gets(str); i=0; while(str[i]) { if ((str[i]>='a')&&(str[i]<='z')) str[i]-=delt; ++i; } printf("The entered string is (in uppercase):\n"); puts(str); return 0; }
By the way, the compiler (or software) I use is SILVERFROST(/PLATO);
AnswerBot
Question subject: Re: convert lowercase to uppercase
Posted: Fri Nov 07, 2008 5:25 pm
Joined: Sun Oct 19, 2008 3:53 pm Posts: 229 Has thanked: 0 time Have thanks: 0 time
The way you wrote the 'for' statement is ingenious, but there's the problem, actually: the 'for' loop executes as long as the condition in the second part between the parentheses holds. You wrote: "loop as long as the i-th character in the string is a lower-case letter." As soon as it encounters a space (or any other character outside that range, for that matter), it exits the loop.
What you really want is to loop through *all* the characters in the string, since you don't know a priori where the last lower-case character is. In other words, you want to loop with 'i' varying from the first index in the string to the last one (which not necessarily will be 80).