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
Find more examples here. C++ simple examples
It is recommended make changes in the source code and examine the output.
This c++ example covers following concepts
- arrays
- const int
- while loop
- switch statement
- cin.getline()
How C++ program works
- Size of array is fixed using the constant variable. User enters a string then a while loop traverse the whole string till null found.
- In while loop using switch statement we check each character of string one by one either it is matching one of case or not.
- 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;
}
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