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 <iostream>
#include <conio.h>
#include <string.h>
#include <stdio.h>
using namespace std;
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];
cout<<"Enter a String: ";
gets(s);
cout<<"Total Number of Vowels = "<<countvowels(s);
return 0;
}
Output
Enter a String: I am learning C++ Total Number of Vowels = 5