given a sentence, I would like to be able to count how many vowels are in each word.
Example input:
Hello this is a test for the program.
Example output:
2 1 1 1 1 1 1 2
My first thoughts were to have 2 while loops. The first that will loop until EOF is met to end the program, and the second (nested) while loop will run until a space (" ") is met, while also summing vowels in current word. Once a space is met it will print out the current value of vowels, the second while loop will end and start again (vowel counter reset back to 0).
This is the code I have written for this:
#include <stdio.h>
#include <ctype.h>
main() {
int vowels = 0;
char c;
while ((c = getchar()) != '\n') {
while((c = getchar()) != " "){
if(tolower(c) =='a' || tolower(c) =='e' || tolower(c) =='i' || tolower(c) =='o' || tolower(c) =='u') vowels++;
}
printf("%d", vowels);
}
}
However this results in syntax error and I cannot figure out my mistake.
line 6:
while ((c = getchar()) != '\n') {
Am I going about this the correct way or is there a different approach I should be taking with this?