Dremendo Tag Line

Input a string and count vowels using a function in C

Function - Question 2

In this question, we will see how to input a string and count how many vowels are there in it in C programming using a function. To know more about function click on the function lesson.

Q2) Write a program in C to input a string and count how many vowels are there in it using a function.

Program

#include <stdio.h>
#include <conio.h>
#include <string.h>

int countvowels(char str[])
{
    int vc=0,i;
    char ch;
    for(i=0; i<strlen(str); i++)
    {
    	ch=tolower(str[i]);
        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        {
            vc=vc+1;
        }
    }
    return vc;
}

int main()
{
    char s[30];
    printf("Enter a String: ");
    gets(s);
    printf("Total Number of Vowels = %d",countvowels(s));
    return 0;
}

Output

Enter a String: I am learning C
Total Number of Vowels = 5
video-poster