59.1k views
2 votes
Write a function that counts and returns the number of vowels in the input, up to the next newline or until the input is done, whichever comes first. Your function should have the following prototype:

int count_vowels();

User Benny Ng
by
4.6k points

1 Answer

2 votes

Answer:

// here is code in C++.

#include <bits/stdc++.h>

using namespace std;

// function that return the number of vowels in the input string

int count_vowels()

{

// variable

string str;

int v_count=0;

cout<<"enter the string:";

// read the string

cin>>str;

// fuind the length

int len=str.length();

// check for vowel

for(int x=0;x<len;x++)

{

char ch=str[x];

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'|ch=='U')

{

v_count++;

}

}

// return the count

return v_count;

}

// driver function

int main() {

// call the function and print the result

cout<<"number of vowels in string is : "<< count_vowels()<<endl;

return 0;

}

Step-by-step explanation:

In the function count_vowels(), read a sting and then find its length.Then check each character of the string is vowel or not.If it is vowel then Increment the v_count. After the loop return the count to main function and print it.

Output:

enter the string: welcometoprogramming

number of vowels in string is : 7

User Shavonne
by
4.7k points