Write a program in C++ that prompts the user to input a sequence of characters and outputs the number of vowels: Use the function we wrote in class that inputs a character and returns true (or 1) if the character is a vowel otherwise it returns a falsehere is what I have so far:

Respuesta :

Answer:

The program to this question can be given as:

Program:

#include <iostream>  //define header file

#include <string>

 //define header file

using namespace std;  

//namespace std;

bool search_Vowel( char cha )  //function definition.

{

return(cha=='a'||cha=='e'||cha=='i'||cha=='o'||cha=='u');//return value.

}

int main()  //main method.

{

   int count=0,i; //define variable.  

   string value;  

   value = "Enter a string value :";  

   cout<<value<< endl;//print value.

   getline(cin,value); //use getline function.

   for (i = 0;i<value.size(); i++ ) //loop

   {

       if (search_Vowel(value[i])) //conditional statement.

       {

           count++; //increase count.

       }

   }

   cout << "In the above string value, there are "<< count <<" vowels." <<endl; //print value.

   return 0;

}

Output:

Enter a string value :

hello..! my name is dev.

In the above string value, there are 6 vowels.

Explanation:

The above program description can be given as below:

  • In the C++ program, firstly we define the header file. After defining the header file we define a function that is "search_Vowel()". In this function, we pass a char variable that is "cha". The data type of this function is string because it returns a string value.  
  • In this function use OR logical operator that checks if any value in the condition is true so, It will return a value.
  • Then we define the main() function in this function we define a variable that is "i, count and value". In which variable i use in the loop and count variable used for count vowels. The value variable is used for taking input from the user. For input, we use the getline() function.  
  • Then we define the for loop in this loop we use the conditional statement in if block we call the search_Vowel in which we pass the value variable.
  • In the above program, the count variable prints the count of vowels.