-->





Translate

count vowels in a string c++

Leave a Comment
Write a C++ program which counts numbers of vowels in a given string and tell every index where a vowel is found.
This c++ example covers following concepts
How C++ program works
  1. Size of array is fixed using the constant variable. User enters a string then a while loop traverse the whole string till null found.
  2. In while loop using switch statement we check each character of string one by one either it is matching one of case or not. 
  3. When a vowel is found check variable value becomes 1 and if condition becomes true and a vowel is found at x index prints on the console.

C++ Source code

#include <iostream>
using namespace std;

int main()
{
    const int SIZE = 100;
    char stringObj[SIZE];
    int index= 0;
    int valueAt = 0;
    int check = 0;

    cout<<"Enter a String to find vowel:   ";
    cin.getline(stringObj,100);
    while(stringObj[index] !='\0')
    {
        switch(stringObj[index])
        {
          case 'a':
              check = 1;

          case 'e':
              check = 1;

          case 'i':
              check = 1;

          case 'o':
              check = 1;

          case 'u':
              check = 1;

          if(check == 1) {
          cout<<"Vowel "<<stringObj[index]<<" found at index number "<<valueAt<<endl;
          }

        check = 0;

        }
    valueAt++;
    index++;
    }

    return 0;
}


count number of vowels in a string c++


Find more examples here. C++ simple examples
It is recommended make changes in the source code and examine the output.

0 comments:

Post a Comment