Write a C++ program which accepts a line of text entered by the user. The whole
ID: 668941 • Letter: W
Question
Write a C++ program which accepts a line of text entered by the user. The whole message must not be stored in the program – instead, it must be read a character at a time, and as each character is read certain properties of the character observed and remembered for later reporting. After the text is read in, the user must be given a choice of these options: 1. Display length of message 2. Display number of non-letter characters (not including the newline character that ends the input) 3. Display numbers of each vowel (upper and lower case counted together) 4. Display numbers of upper and lower case letters 5. Quit The result of the user’s selection must be displayed, and the menu repeated, until 5 for Quit is chosen.
Requirements:
Read in the text entered by user using cin.get in a loop something like this: do { cin.get(current_symbol); // check properties of current_symbol and add up various counts as needed here... } while (current_symbol != ’ ’); Note: The purpose of using cin.get(current symbol); rather than cin >> current symbol; is so that spaces will not be skipped over, but rather treated just like any other character.
• If an empty message is entered, the user should be prompted to enter a non-empty message until they enter one which is at least 1 character long. (Any character that is not a ’ ’ would count, even a space.)
• In counting the numbers of vowels, both upper and lower case letters should be counted together (e.g. for “Edible egg” the count for ‘e’ should be 3).
• Use comments to show the main steps in the program, and to document variable declarations
Explanation / Answer
#include <iostream>
using namespace std;
#include<stdio.h>
#include <ctype.h>
int main()
{
int length = 0, nNonlet = 0, nUpper = 0, nLower = 0;
int option = 0;
int nVowels[5] = {0,0,0,0,0}; // in the order a,e,i, o, u
char ch, temp;
cout << "Enter one character at a time. Use ' ' to terminate "<<endl;
while(ch !=' ') {
ch = cin.get();
if(ch == ' ') {
if(nUpper >0 || nLower >0 || nNonlet >0)
break;
else {
cout << " Enter atleast one character - No empty string"<<endl;
continue;
}
}
length++;
//check letter or not
if(isalpha(ch)) {
//if upper case letter
if(isupper(ch))
nUpper++;
else
nLower++; //lower case letter
temp = tolower(ch);
//check whether vowel or not
if( temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u') {
int index =0; // for 'a'
if(temp == 'e')
index = 1;
else if(temp == 'i')
index = 2;
else if(temp == 'o')
index = 3;
else if(temp == 'u')
index = 4;
nVowels[index]++;
}
} else {
//if non-letter increase the count
nNonlet++;
}
cout << ch << " ";
}
cout <<endl;
cout << " 1. Display length of message 2. Display number of non-letter characters 3. Display numbers of each vowel (upper and lower case counted together) 4. Display numbers of upper and lower case letters 5. Quit " <<endl;
while(option !=5) {
cout << "Enter option" <<endl;
cin >> option;
if(option == 1)
cout << "length of message : " << length <<endl;
if(option == 2)
cout << "number of non-letter characters : " << nNonlet <<endl;
if(option == 3) {
cout << "Vowel- a/A , count : " << nVowels[0] <<endl;
cout << "Vowel- e/E , count : " << nVowels[1] <<endl;
cout << "Vowel- i/I , count : " << nVowels[2] <<endl;
cout << "Vowel- o/O , count : " << nVowels[3] <<endl;
cout << "Vowel- u/U , count : " << nVowels[4] <<endl;
}
if(option == 4) {
cout << "number of upper case characters : " << nUpper<<endl;
cout << "number of lower case characters : " << nLower<<endl;
cout << "number of letters (both upper and lower case) : " << (nUpper+nLower)<<endl;
}
if(option == 5)
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.