
Read a string and count the number of vowels,consonants and spaces in C
Algorithm
Input: Input a string
output: Displays the number of vowels, consonant and spaces
- Start
- scount = 0, vcount = 0, ccount = 0, i = 0
- Read a string from the user say str
- while ( str[i] != '\0')
4.1 if str[i] = = 'a' || str[i] = = 'e' || str[i] = = 'i' || str[i] = = 'o' || str[i] = = 'u'
4.1.1 vcount++
4.2 else if str[i]==' '
4.2.1 scount++
4.3 else
4.3.1 ccount++ - Print "The number of vowels in the string is",vcount
- Print "The number of consonants in the string is",ccount
- Print "The number of spaces in the string is",scount
- Stop
C Program
#include<stdio.h>
#include<string.h>
int main()
{
int scount=0,vcount=0,ccount=0;
char str[30];
printf("Enter the string:");
gets(str);
for(int i=0;str[i]!='\0';i++) {
if (str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'){
vcount++;
}
else if (str[i]==' '){
scount++;
}
else {
ccount++;
}
}
printf("The number of vowels in the string is: %d\n",vcount);
printf("The number of consonants in the string is: %d\n",ccount);
printf("The number of spaces in the string is: %d\n",scount);
return 0;
}
Output
Enter the string:hello world
The number of vowels in the string is: 3
The number of consonants in the string is: 7
The number of spaces in the string is: 1
Comments