Question
Use C++ language, keep it simple
Exercise #2: Strings operations Write a C++ program that prompts the user to enter a sentence, the program then does the fol
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The logic is explained in comments.

CODE:


#include <iostream>
#include <string>

using namespace std;


int main()
{
string sentence; // to store input from user
int characterCount = 0; // to count characters in the sentence
int wordCount = 0; // to count words in sentence
int vowelTotalCount = 0; // to count vowel in sentence
int vowelCount[5] = {0}; // to count vowel frequency in array
char ch; // current character in the sentence
int nonAlphabetCount = 0; // to count non albhabet in sentence
int index = 0;
  
// prompt to enter a sentence and use getline method to get whole sentence
cout << "Enter a string : ";
getline(cin, sentence);
  
// convert the entered string to character pointer
const char *strSentence = sentence.c_str();
const char *tempStr = strSentence; // to keep the start pointer
  
// make ch to point to first character in sentence
ch = *strSentence;
  
// loop till null character (end of string) not found in the sentence
while(ch != '\0')
{
// increment the character count
characterCount++;
  
// character is a space, increment word count
if(ch == ' ')
wordCount++;
  
// character is non albhabet, increment non albhabet count
if(!isalpha(ch))
nonAlphabetCount++;
  
// character is a vowel increment vowel frequency and total vowel count
if(ch == 'a' || ch == 'A')
{
vowelCount[0]++; // increment frequency count
vowelTotalCount++; // increment total count
}
else if(ch == 'e' || ch == 'E')
{
vowelCount[1]++; // increment frequency count
vowelTotalCount++; // increment total count
}
else if(ch == 'i' || ch == 'I')
{
vowelCount[2]++; // increment frequency count
vowelTotalCount++; // increment total count
}
else if(ch == 'o' || ch == 'O')
{
vowelCount[3]++; // increment frequency count
vowelTotalCount++; // increment total count
}
else if(ch == 'u' || ch == 'U')
{
vowelCount[4]++; // increment frequency count
vowelTotalCount++; // increment total count
}
  
// move the string pointer to next character
strSentence++;
  
// get the current character
ch = *strSentence;
}
  
// increment the word count as last word was not got counted in loop
wordCount++;
  
// diaplay character and word count
cout << "Character Count : " << characterCount << endl;
cout << "Word Count : " << wordCount << endl;
cout << "Vowel Count : " << vowelTotalCount << endl;
  
// display vowel frequency
cout << "Vowel Frequency :" << endl;
cout << "a\te\ti\to\tu" << endl;
for(index = 0; index < 5; index++)
{
for(int tabIndex = 0; tabIndex < index; tabIndex++)
{
cout << "\t";
}
cout << vowelCount[index] << endl;
}
  
// display reverse
strSentence = tempStr; // make strSentence to point to start of sentence
cout << "Reverse : ";
// print from last character
for(index = characterCount - 1; index >= 0; index--)
{
cout << strSentence[index];
}
cout << endl;
  
// display non alphabet count
cout << "Non-alphabet Count : " << nonAlphabetCount << endl;
  
// display change case
strSentence = tempStr; // make strSentence to point to start of sentence
cout << "Change Case : ";
ch = *strSentence; // make ch to point to first character
while(ch != '\0')
{
// if character is lower make it upper
if(ch >= 'a' && ch <= 'z')
cout << (char)('A' + ch - 'a');
// if character is upper make it lower
else if(ch >= 'A' && ch <= 'Z')
cout << (char)('a' + ch - 'A');
// otherwise print as it is
else
cout << ch;
  
strSentence++; // increment string pointer
ch = *strSentence; // make ch to point to current character
}

return 0;
}

#include <iostream> #include <string> using namespace std; int main() string sentence; int characterCount = 0; int wordCount

// loop till null character (end of string) not found in the sentence while(ch != \0) // increment the character count char

vowelCount [2]++; vowelTotalCount++; // increment frequency count // increment total count else if(ch == o || ch == 0) vo

cout << Vowel Count : << vowelTotalCount << endl; // display vowel frequency cout << Vowel Frequency : << endl; cout <<

// display change case strSentence = tempStr; // make strSentence to point to start of sentence cout << Change Case : ; ch

OUTPUT:

Enter a string : Dr. Ali likes to play football, tennis, and golf. Character Count : 49 Word Count : 9 Vowel Count : 13 Vowel

Add a comment
Know the answer?
Add Answer to:
Use C++ language, keep it simple Exercise #2: Strings' operations Write a C++ program that prompts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first...

    PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first word which has its reverse word further ahead in the string. Input Format: A string of space separated words, ending with a 's'. Conditions ; Each string ends with a $ character. All characters in the string are either spaces. $ or lower case English alphabets, Output Format : In a single line pent either . The first word from the start of the...

  • String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

    String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak....

  • c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence,...

    c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence, then counts and prints the number of words in the sentence. Assume that there is more than one space between words of the sentence. Sample input/output: Enter a sentence: This is a 123test in There are 5 words in " This is a 123test \n"

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

  • Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words...

    Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words [Q]uit 1. For Erase–ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase is as follows: void Erase( int a[ ], int * N, int * Search-Element ) The function Erase should remove all occurrences of Search-Element from the array    a[ ]. Note that array a[ ] is loaded with integer numbers entered by the user through the...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit...

    Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit 1. For Erase-ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase function is as follow: void Erase( int a[ ], int * N, int * Search-Element) The function Erase should remove all occurrences of Search-Element from the array al). Note that array a[ ] is loaded with integer numbers entered by the user through the keyboard. N...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT