Count Vowels in a String – C Program


Write a C program to count the number of vowels in a given string and display all the vowels in it. [Dec 2015, Set-1]

#include <stdio.h>

int main()
{
    char str[80];
    int i, count= 0;

    printf("Enter a string: ");
    gets(str);

    for (i = 0; str[i] != '\0'; i++)
    {
        /* Checking for vowels */
        if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
        {
            count++;
        }
    }

    printf("The number of vowels = %d", count);

    return 0;
}