Working with characters Write a C program which accepts a li
Working with characters:
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 numbers of each vowel (upper and lower case counted together)
3.Display numbers of upper and lower case letters
4.Quit
The result of the user’s selection must be displayed, and the menu repeated, until 4 for Quit is chosen.
Requirements:
1.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.
2.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.)
3.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).
4.Use comments to show the main steps in the program, and to document variable declarations.
5.A sample run of your program should look like:
Enter a message: Eucalyptus is evergreen.
Available choices:
1. Display length of message
2. Display numbers of vowels
3. Display numbers of upper and lower case letters
4. Quit
Enter the number of your choice: 2
Vowel counts:
A’s: 1, E’s: 5, I’s: 1, O’s: 0, U’s: 2
Enter the number of your choice: 1 Your message contained 24 character(s).
Available choices:
1. Display length of message
2. Display numbers of vowels
3. Display numbers of upper and lower case letters
4. Quit
Enter the number of your choice: 3
Number of letters: upper case 1, lower case 20.
Available choices:
1. Display length of message
2. Display numbers of vowels
3. Display numbers of upper and lower case letters
4. Quit
Enter the number of your choice: 4
Hints:
•Use general expressions that have the meaning if (symbol is a lower case letter) and if (symbol is an upper case letter).
•Checking for and counting the vowels is most easily done using a switch statement.
•Make sure that you do not count the newline (’\ ’) character obtained at the end of the input in the length of the message.
Solution
#include <cctype>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <conio.h>
using namespace std;
char menu_function(); //function that will run the menu to call the other functions.
void sub_function(char[], char, char); //function that will be used to substitute charachters.
int vowel_function(char[], int); //function that will be used to count the vowels.
int main()
{
char strg_array[80];
char contin = \' \';
char choice = \' \';
char target = \' \';
char new_val = \' \';
int array_size = 80;
int vowel_sum = 0;
do
{
system(\"cls\");
cout << \"Please enter a string. \";
cin.getline(strg_array,80);
cout << endl << endl;
choice = menu_function();
/*
if(islower(choice)) //this is supposed to test if the menu choice was entered in lower case
{
char(toupper(choice)); // this is supposed to change the entered menu choice to uppercase if it was originaly lowercase,
// but this doesnt work
}
*/
switch(choice)
{
case \'A\':
case \'a\':
cout << \" Please select a character that you wish to substitute. \";
cin >> target;
cout << endl << endl << \"Please select a character that you wish to use in the other characters place. \";
cin >> new_val;
cout << endl << endl;
sub_function(strg_array, target, new_val);
break;
case \'B\':
case \'b\':
vowel_sum = vowel_function(strg_array, array_size);
if(vowel_sum == 0)
{
cout << \" The string does not consist of any vowel.\ \ \";
}
else
{
cout << \" There were a total of \" << vowel_sum << \" vowel(s) in the string entered.\ \ \";
}
break;
}
cout << \"If you would like to continue and enter another string please press Y,\ if you would like to exit the program enter any other character. \";
contin = getche();
cout << endl << endl;
}while((contin == \'Y\') || (contin == \'y\'));
return 0;
}
char menu_function()
{
char option = \' \';
do
{
cout << \"MENU\ \";
cout << \"A - Subsitute Character\ \";
cout << \"B - Count Vowels\ \";
cout << \"Please select an option from the menu.\";
cin >> option;
cout << endl << endl;
}while((option != \'A\') && (option != \'a\') && (option != \'B\') && (option != \'b\'));
return option;
}
void sub_function(char* ch_ptr, char tar, char n_val)
{
while(*ch_ptr != \'\\0\')
{
if(*ch_ptr == tar)
{
*ch_ptr = n_val;
}
++ch_ptr;
}
return;
}
int vowel_function(char ch_array[], int size)
{
char* ch_ptr = ch_array;
int a = 0;
int e = 0;
int i = 0;
int o = 0;
int u = 0;
int length = 0;
int sum = 0;
int non_vowel = 0;
while(*ch_ptr != \'\\0\')
{
if((*ch_ptr == \'a\') || (*ch_ptr == \'A\'))
++a;
else if((*ch_ptr == \'e\') || (*ch_ptr == \'E\'))
++e;
else if((*ch_ptr == \'i\') || (*ch_ptr == \'I\'))
++i;
else if((*ch_ptr == \'o\') || (*ch_ptr == \'O\'))
++o;
else if((*ch_ptr == \'u\') || (*ch_ptr == \'U\'))
++u;
++ch_ptr;
}
sum = a + e + i + o + u;
length = strlen(ch_array);
non_vowel = length - sum;
cout << \" There were \" << a << \" A(s) in your string.\ \";
cout << \" There were \" << e << \" E(s) in your string.\ \";
cout << \" There were \" << i << \" I(s) in your string.\ \";
cout << \" There were \" << o << \" O(s) in your string.\ \";
cout << \" There were \" << u << \" U(s) in your string.\ \";
cout << \" There were \" << non_vowel << \" non_vowel alphebetic characters in your string.\ \ \";
return sum;
}





