#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[1000];
int i, letter, space = 0;
char ch = str1[i];
printf("Enter a sentence: ");
scanf("%[^\n]s", str1);
printf("you enter %s\n", str1);
while (i != strlen(str1)) {
if (ch != ' ') {
letter++;
} else if (ch = ' ') {
space++;
}
i++;
}
printf("%d %d", letter, space);
}
My while loop isn't working and I can't seem to locate the problem. I am using the terminal in ubuntu and after printing the user string, I get a blank line. I have to use Ctrl-Z to stop the script.
iisn't initializedchar ch = str1[i];this needs to be inside the loop. As now,iandstrare uninitilaised.}else if (ch=' '){is unnecessary, but it's also incorrect. You should be using==instead of=here.char ch = str1[i];supposed to do?