C++ Program to count the number of vowels in a string [DEVCPP/GCC]


STEPS 

1. Initialize the count with 0.

2. Input the string, in this case a[10].

3. Initialize i with 0, check the value at a[i] against the cases mentioned in switch loop.

4. If the value tends to be equal to any of the case labels , increment the count.

5. Increment i by 1 and repeat steps 3 and 4 till a[i] is not equal to '\0' (null).

6. Display count.


PROGRAM

//Program to count the number of vowels in a string

# include<iostream>

using namespace std;

int main()
{
char a[10];
int i,j,count=0;

cin.getline(a,20); //Input the string

for(i=0;a[i]!='\0';i++)    // Loop to count the number of vowels
{
switch(a[i])
{
 
                case  'a' :  case 'A':
                case  'i'  :  case 'I':
                case  'e' :  case 'E':
                case  'o' :  case 'O':
                case  'u' :  case 'U':
             
                 count++ ;
}
}

cout<<count;   // Displaying count
return 0; 
}

OUTPUT

C++ Program to count the number of vowels in a string with output

Share this

Related Posts

FIND US ON FACEBOOK!