In: Computer Science
C++
(1) Prompt the user to enter a string of their choosing
(Hint: you will need to call the
getline() function to read a string consisting of white
spaces.) Store the text in a string. Output the string. (1
pt)
Ex:
Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
(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.
More specifically, the PrintMenu() function will consist of the following steps:
Ex:
MENU c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit Choose an option:
(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.
Ex:
Number of non-whitespace characters: 181
(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.
Ex:
Number of words: 35
(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.
Ex:
Enter a word or phrase to be found: more "more" instances: 5
(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 return the revised string. Call
ReplaceExclamation() in the PrintMenu() function, and then output
the edited string.
Ex.
Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue.
(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
return the revised string. Call ShortenSpace() in the PrintMenu()
function, and then output the edited string.
Ex:
Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
______________________________________________
#include <iostream>
#include <string>
using namespace std;
int main() {
/* Type your code here. */
return 0;
}
If you have any doubts, please give me comment...
#include <iostream>
#include <cstdlib>
using namespace std;
int GetNumOfNonWSCharacters(string str)
{
int i = 0, len = str.length(), count = 0;
while (i < len)
{
if (str[i] != ' ')
count++;
i++;
}
return count;
}
int GetNumOfWords(string str)
{
int i = 0, len = str.length(), count = 0, prev = 0;
while (i < len)
{
if (str[i] == ' ' && str[i - 1] != ' ')
{
count++;
}
i++;
}
return count + 1;
}
int FindText(string str, string word)
{
int i = 0, len = str.length(), instances = 0, w_len = word.length();
while (i < len - w_len)
{
int j = 0, count = 0;
while (j < w_len)
{
if (str[i + j] == word[j])
{
count++;
}
j++;
}
if (count == w_len)
{
instances++;
}
i++;
}
return instances;
}
void ReplaceExclamation(string *str)
{
int i = 0, len = (*str).length();
while (i < len)
{
if ((*str)[i] == '!')
{
(*str)[i] = '.';
}
i++;
}
}
void ShortenSpace(string *str)
{
int i = 0, len = (*str).length();
while (i < len)
{
if ((*str)[i] == ' ' && (*str)[i + 1] == ' ')
{
cout << "in " << endl;
int j = i;
while (j < len)
{
(*str)[j] = (*str)[j + 1];
j++;
}
(*str)[j] = '\0';
len--;
}
i++;
}
}
void PrintMenu(string &str)
{
char choice;
do
{
cout << "MENU" << endl;
cout << "c - Number of non-whitespace characters" << endl;
cout << "w - Number of words" << endl;
cout << "f - Find text" << endl;
cout << "r - Replace all !'s" << endl;
cout << "s - Shorten spaces" << endl;
cout << "q - Quit" << endl;
cout << "Choose an option: ";
cin >> choice;
switch (choice)
{
case 'c':
cout << "Number of non-whitespace characters: " << GetNumOfNonWSCharacters(str) << endl;
break;
case 'w':
cout << "Number of Words: " << GetNumOfWords(str) << endl;
break;
case 'f':
{
string word;
cout << "Enter a word or phrase to be found: ";
getline(cin, word);
getline(cin, word);
cout << "\"" << word << "\" instances: " << FindText(str, word) << endl;
break;
}
case 'r':
ReplaceExclamation(&str);
cout << "Edited text: " << str << endl;
break;
case 's':
ShortenSpace(&str);
cout << "Edited text: " << str << endl;
break;
case 'q':
cout << "Thank you!" << endl;
exit(1);
default:
cout << "Invalid option" << endl;
}
}while(choice!='q');
}
int main()
{
string str;
cout << "Enter a sample text: " << endl;
getline(cin, str);
cout << "You entered: " << str << endl;
PrintMenu(str);
return 0;
}