(1) Prompt the user to enter a string of their choosing. Store the text in a str
ID: 3717970 • Letter: #
Question
(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)
Ex:
(2) Implement a PrintMenu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to call PrintMenu() until the user enters q to Quit. (3 pts)
Ex:
(3) Implement the GetNumOfNonWSCharacters() function. GetNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. Call GetNumOfNonWSCharacters() in the PrintMenu() function. (4 pts)
Ex:
(4) Implement the GetNumOfWords() function. GetNumOfWords() has a constant string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call GetNumOfWords() in the PrintMenu() function. (3 pts)
Ex:
(5) Implement the FindText() function, which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In the PrintMenu() function, prompt the user for a word or phrase to be found and then call FindText() in the PrintMenu() function. Before the prompt, call cin.ignore() to allow the user to input a new string. (3 pts)
Ex:
(6) Implement the ReplaceExclamation() function. ReplaceExclamation() has a string parameter and updates the string by replacing each '!' character in the string with a '.' character. ReplaceExclamation() DOES NOT output the string. Call ReplaceExclamation() in the PrintMenu() function, and then output the edited string. (3 pts)
Ex.
(7) Implement the ShortenSpace() function. ShortenSpace() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. ShortenSpace() DOES NOT output the string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string. (3 pt)
Ex:
Explanation / Answer
Outputstring.cpp
//header files
#include"stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//Function prototypes
char printMenu(string);
int GetNumOfNonWSCharacters(string);
int GetNumOfWords(string);
void ReplaceExclamation(string&);
void ShortenSpace(string&);
int FindText(string, string);
//Main function
int main()
{
//variable decalaration
char option;
string text, phraseToFind;
//Reading text from user
cout << " Enter a sample text: ";
getline(cin, text);
//Printing text
cout << " You entered: " << text;
//Loop till user wants to quit
do
{
//Printing menu
option = printMenu(text);
cout << " ";
} while (option == 'Q' || 'q');
system("pause");
return 0;
}
//Function that prints menu
char printMenu(string text)
{
char ch;
string phraseToFind;
//Printing menu
cout << " Menu Options: ";
cout << " c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit";
cout << " Choose an option: ";
//Reading user choice
cin >> ch;
//Calling functions based on option selected by //user
switch (ch)
{
//User wants to quit
case 'q':
case 'Q':
exit(0);
//Counting non-whitespace characters
case 'c':
case 'C':
cout << " Number of non-whitespace characters: " << GetNumOfNonWSCharacters(text) << " ";
break;
//Counting number of words
case 'w':
case 'W':
cout << " Number of words: " << GetNumOfWords(text) << " ";
break;
//Counting number of occurrences phrase in //given string
case 'f':
case 'F':
cin.ignore();
cout << " Enter a word/Phrase to find: ";
getline(cin, phraseToFind);
cout << " "" << phraseToFind << "" instances: " << FindText(text, phraseToFind) << " ";
break;
//Replacing ! with .
case 'r':
case 'R': ReplaceExclamation(text); cout << " Edited text: " << text << " ";
break;
//Replacing multiple spaces with single //space
case 's':
case 'S': ShortenSpace(text); cout << " Edited text: " << text << " ";
break;
default:
cout << " Invalid Choice.... Try Again ";
break;
}
return ch;
}
//Function that count number of non space characters
int GetNumOfNonWSCharacters(const string text)
{
int cnt = 0, i;
int len = text.size();
//Looping over given text
for (i = 0; i<len; i++)
{
//Counting spaces
if (!isspace(text[i]))
cnt++;
}
return cnt;
}
//Function that count number of words in the string
int GetNumOfWords(const string text)
{
int words = 0, i;
int len = text.size();
//Looping over text
for (i = 0; i<len;)
{
//Checking for space
if (isspace(text[i]))
{
//Handling multiple spaces
while (isspace(text[i]))
i++;
//Incrementing words
words++;
}
else
{
i++;
}
}
//Handling last word
words = words + 1;
return words;
}
//Function that replaces ! with .
void ReplaceExclamation(string& text)
{
string newText = text;
int i, len = text.size();
//Looping over string
for (i = 0; i<len; i++)
{
//Replacing ! with .
if (text[i] == '!')
newText[i] = '.';
}
text = newText;
}
//Function that replaces Multiple spaces with single space
void ShortenSpace(string& text)
{
char *newText;
int i, len = text.size(), k = 0;
newText = new char[len + 1];
//Looping over string
for (i = 0; i<len; k++)
{
//Assign individual characters
newText[k] = text[i];
//Handling multiple spaces
if (isspace(text[i]))
{
//Replacing multiple spaces with single //space
while (isspace(text[i]))
i++;
}
else
{
i++;
}
}
newText[k] = '';
text = newText;
}
//Function that counts the occurrences of given phrase in a //given text
int FindText(string text, string phrase)
{
int count = 0;
if (phrase.size() == 0)
return 0;
//Counting number of phrase occurrences in the given string
for (size_t offset = text.find(phrase); offset != string::npos; offset = text.find(phrase, offset + phrase.size()))
{
++count;
}
//Retuning count
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.