Input sentence and print each word with number of vowels present in it in C
String Methods - Question 9
In this question, we will see how to input a sentence and print each word along with the number of vowels present in it in C programming. To know more about string methods click on the string methods lesson.
Q9) Write a program in C to input a sentence and print each word along with the number of vowels present in it.
Program
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char s[100],w[30];
char ch;
int i,j,vc,p=0;
printf("Enter a sentence\n");
gets(s);
strcat(s," "); // add a space so that we can get the last word
for(i=0; i<strlen(s); i++)
{
if(s[i]!=' ')
{
// store the characters in array w until we find a space
w[p]=s[i];
p++;
}
else
{
// terminate the word by a null character
w[p]='\0';
vc=0;
for(j=0; j<strlen(w); j++)
{
ch=tolower(w[j]);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
vc++;
}
}
printf("%s = %d\n",w,vc);
p=0;
}
}
return 0;
}
Output
Enter a sentence My hobby is to read novels and watch movies My = 0 hobby = 1 is = 1 to = 1 read = 2 novels = 2 and = 1 watch = 1 movies = 3