Write C++ code that reads lower case letters of the alphabet (assume they are co
ID: 3760727 • Letter: W
Question
Write C++ code that reads lower case letters of the alphabet (assume they are coming from the input stream, e.g. y x v o ...), one character at a time. If the character is a vowel, i.e. one of {a,e,i,o,u}, is pushed onto a stack, otherwise it is ignored. When there is no more input, print out the characters on the stack so that they are output in the reverse order in which they appeared inthe input. In all cases, you must use loops, as opposed to having repeated statements, to process the letters.
Explanation / Answer
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int temp;
char c;
stack<char> characters;
while (true) {
cout << "Input a lowercase character or press any digit to exit: ";
cin >> c;
temp = c;
if(temp>=97 && temp<=122)
{
if(c=='a' || c == 'e' || c == 'i' || c == 'o' || c=='u')
characters.push(c);
}
else if(temp>=48 && temp<=57)
{
cout << "Exitting the program. ";
break;
}
else
cout << "Please input a lowercase character. ";
}
cout << "Printing stack contents : ";
while(!characters.empty())
{
c = characters.top();
cout << c << " ";
characters.pop();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.