how to solve this question on C++ using string Exercise #3: Strings\' operations
ID: 3741028 • Letter: H
Question
how to solve this question on C++ using string
Exercise #3: Strings' operations Write a C+ program that prompts the user to enter a sentence, the program then does the following 1. 2. 3. 4. 5. 6. 7. Prints the total characters in the sentence Prints the word count of the sentence, assuming only one space between each tow words Prints total vowel { a. e. i, o, u} in the sentence Prints the vowel frequency as shown in the sample below Prints the sentence in reverse Prints the total number of characters other than the alphabets (Note: space is a character) Prints the sentence in a way so that the case of each character is changed. i.e· from lower to upper and vice versa Sample input / output: nter a string: Dr. Al haracter Count49 ord Count : 9 owel Count :13 owel Frequency: kes to play football, tennis, and golf. 2 0 everse : .flog dna .sinnet .llabtoof yalp ot sekil ilA .rlD on-aphabet Count 12 hange Case : dR. aLI LIKES TO PLAY FOOTBALL. TENNIS. AND GOLFExplanation / Answer
#include <iostream>
#include <string>
using namespace std;
void reverse(const string& a);
int main()
{
string str;
int words = 0; // Holds number of words
int vowelCount = 0; //Holds vowel count
int vowelCountA = 0; //Holds vowelA count
int vowelCountE = 0; //Holds vowelE count
int vowelCountI = 0; //Holds vowelI count
int vowelCountO = 0; //Holds vowelO count
int vowelCountU = 0; //Holds vowelU count
int nonAlphabetCount = 0; //Holds non alphabet count count
cout << "Enter a string : ";
getline (cin, str); // read a string
// 1. Total characters in sentence
cout << "Character Count : " << str.size() << endl;
// 2. Word count of sentence
for(int i = 0; str[i] != ''; i++)
{
if (str[i] == ' ') //Checking for spaces
{
words++;
}
}
cout << "Word Count : " << words+1 << endl;
//3. Vowel count
//4. Vewel frequency count
for(int i = 0; str[i] != ''; i++)
{
if (str[i] == 'a' || str[i] == 'A' || str[i] == 'e' || str[i] == 'E' ||
str[i] == 'i' || str[i] == 'I' || str[i] == 'o' || str[i] == 'O' ||
str[i] == 'u' || str[i] == 'U' )
{
vowelCount++;
}
if (str[i] == 'a' || str[i] == 'A'){ // Checking for vowel 'a' or 'A'
vowelCountA ++;
}
if (str[i] == 'e' || str[i] == 'E'){ // Checking for vowel 'e' or 'E'
vowelCountE ++;
}
if (str[i] == 'i' || str[i] == 'I'){ // Checking for vowel 'i' or 'I'
vowelCountI ++;
}
if (str[i] == 'o' || str[i] == 'O'){ // Checking for vowel 'o' or 'O'
vowelCountO ++;
}
if (str[i] == 'u' || str[i] == 'U'){ // Checking for vowel 'u' or 'U'
vowelCountU ++;
}
}
cout << "Vowel Count : " << vowelCount << endl;
cout << "Vowel Frequency : " << endl;
cout << "a " << " e " << " i "<< " o "<<" u "<< endl;
cout << vowelCountA << endl;
cout << " "<<vowelCountE << endl;
cout << " "<<vowelCountI << endl;
cout << " "<<vowelCountO << endl;
cout << " "<<vowelCountU << endl;
//5. Sentense in reverse
cout<<"Reverse : ";
reverse(str);
//6. Total number of characters other than alphabets
for(int i = 0; str[i] != ''; i++)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
continue;
}
else
{
nonAlphabetCount++;
}
}
cout<< "Non-Alphabet Count : "<<nonAlphabetCount<<endl;
//7. Change Case Lower to Upper and vice versa
for(int i = 0; str[i] != ''; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}else if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
}
cout<< "Case Change : "<<str<<endl;
return 0;
}
void reverse(const string& str)
{
size_t numOfChars = str.size();
if(numOfChars == 1)
cout << str << endl;
else
{
cout << str[numOfChars - 1];
reverse(str.substr(0, numOfChars - 1));
}
}
********** Output ************
Enter a string : Dr. Ali likes to play football, tennis, and golf.
Character Count : 49
Word Count : 9
Vowel Count : 13
Vowel Frequency :
a e i o u
4
2
3
4
0
Reverse : .flog dna ,sinnet ,llabtoof yalp ot sekil ilA .rD
Non-Alphabet Count : 12
Case Change : dR. aLI LIKES TO PLAY FOOTBALL, TENNIS, AND GOLF.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.