C program to count number of vowels in a string: for example, in the string "love," there are two vowels, 'o' and 'e,'. In the program, both lower-case and upper-case are considered, i.e., 'a,' A,' 'e,' 'E,' 'i', 'I,' 'o,' O,' 'u', and 'U'. In this program, we check every character in the input string. If it's a vowel, the counter (variable used to count the number of vowels) is incremented by one, and consonants and special characters are ignored.
Count vowels in a string in C
int main()
{
int c = 0, count = 0;
char s[1000];
printf("Input a string\n");
gets(s);
while (s[c] != '\0') {
if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' || s[c] == 'I' || s[c] =='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U')
count++;
c++;
}
printf("Number of vowels in the string: %d", count);
return 0;
}
Output of C program to count number of vowels in a string:
C program to count vowels in a string using function
int count_vowels(char []);
int check_vowel(char);
int main()
{
char array[100];
int c;
printf("Enter a string\n");
gets(array);
c = count_vowels(array);
printf("Number of vowels: %d\n", c);
return 0;
}
int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;
do
{
d = a[c];
flag = check_vowel(d);
if (flag == 1)
count++;
c++;
} while (d != '\0');
return count;
}
int check_vowel(char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return 1;
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
return 1;
return 0;
}