Question

C++ please! (1) Prompt the user to enter the name of the input file. The file...

C++ please!

(1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string.

Ex:

Enter file name:
input1.txt

File content: 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.

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. (4 pts)

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 output the 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 output the 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!


(8) If the input file does not exist, inform the user and quit the program.

Ex:

Enter file name:
noinput.txt

File not found.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE

nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE File Edit View Search Terminal Help nikhil@nikhil-Vostro-15-3568:-/Desktop/CODES

nikhil@nikhil-Vostro-15-3568: -/Desktop/CODE File Edit View Search Terminal Help 5. Replace all !s s Shorten spaces 9 - Quit

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int GetNumOfNonWSCharacters(string s)
{
int count = 0;
for (int i = 0; s[i] != '\0'; i++)
{
if (s[i] != ' ')
count++;
}
return count;
}

int GetNumOfWords(string str)
{
int count = 0;
string word = "";
for (auto x: str)
{
if (x == ' ')
{
count++;
word = "";
}
else
{
word = word + x;
}
}
count++;
return count;
}

int FindText(string find, string str)
{
int count = 0;
string word = "";
for (auto x: str)
{
if (x == ' ')
{
if (word == find)
count++;
word = "";
}
else
{
word = word + x;
}
}
if (word == find)
count++;
return count;
}

void ReplaceExclamation(string s)
{
for (int i = 0; s[i] != '\0'; i++)
{
if (s[i] == '!')
s[i] = '.';
}
cout<<"Edited text: "<<s<<endl;
}

void ShortenSpace(string str)
{
size_t first = str.find_first_not_of(' ');
size_t last = str.find_last_not_of(' ');
str = str.substr(first, (last - first + 1));
cout<<"Edited text: "<<str<<endl;
}

void PrintMenu(string s)
{   
string find;
while(true){
cout << "MENU \nc - Number of non-whitespace characters \nw - Number of words \nf - Find text \nr - Replace all !'s \ns - Shorten spaces \nq - Quit \nChoose an option: ";
char c;
cin >> c;
switch(c){
case 'c':
cout<<"Number of non-whitespace characters: "<<GetNumOfNonWSCharacters(s)<<endl;
break;

case 'w':
cout<<"Number of words: "<<GetNumOfWords(s)<<endl;
break;

case 'f':
cout<<"Enter a word or phrase to be found: ";
cin.ignore();
getline(cin,find);
cout<<"'"<<find<<"'"<<" instances: "<<FindText(find,s)<<endl;
break;

case 'r':
ReplaceExclamation(s);
break;

case 's':
ShortenSpace(s);
break;   

case 'q':
return;

default:
cout<<"Invalid Input\n";
}
}
}

int main()
{
string file;
cout << "Enter file name: ";
cin >> file;
fstream newfile;
newfile.open(file, ios:: in);
if (newfile.is_open())
{

string tp;
getline(newfile, tp);
newfile.close();
PrintMenu(tp);
}
else
{
cout << "File not found.\n";
}
}

/* A.java } } -/Desktop/CODE/demo.cpp (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project P

-/Desktop/CODE/demo.cpp (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Hel

/* A.java --/Desktop/CODE/demo.cpp (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Pref

Add a comment
Know the answer?
Add Answer to:
C++ please! (1) Prompt the user to enter the name of the input file. The file...
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
  • C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...

    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!...

  • Program: Authoring assistant

    7.17 LAB*: Program: Authoring assistant(1) Prompt the user to enter a string of their choosing. 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() method, which 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() method....

  • (1) Prompt the user to enter a string of their choosing. Store the text in a...

    (1) Prompt the user to enter a string of their choosing. 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...

  • (7) Implement the shorten_space() function. shorten_space() has a string parameter and updates the string by replacing...

    (7) Implement the shorten_space() function. shorten_space() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. shorten_space() returns the string. Call shorten_space() in the print_menu() function, and then output the edited string. Hint: Look up and use Python function .isspace(). 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....

  • The code keeps saying its generating too much output check for any unterminated loops. I cant...

    The code keeps saying its generating too much output check for any unterminated loops. I cant find any. Can anyone help? #include <iostream> #include <string> using namespace std; char printMenu(); int GetNumOfNonWSCharacters(string); int GetNumOfWords(string); void ReplaceExclamation(string&); void ShortenSpace(string&); int FindText(string, string); int main(){ char option; string text, phraseToFind; cout << "\n\n Enter a sample text: "; getline(cin, text); cout << "\n\n You entered: " << text; while(1){ option = printMenu(); switch(option){ case 'q': case 'Q': return 0; case 'c': case...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • 6.17.1 Lab #5 - Chapter 6 Text analyzer & modifier (C) (1) Prompt the user to...

    6.17.1 Lab #5 - Chapter 6 Text analyzer & modifier (C) (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the GetNumOfCharacters() function, which returns the number of characters in the user's string. We encourage you to use a for loop in this...

  • This program should be run on Visual Studio. Please use printf and scanf as input and...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.11 Lab Exercise Ch.6a: Functions: String analyzer Create and debug this program in Visual Studio. Upload your Source.cpp file for testing (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: ics Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have...

  • ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file...

    ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file that contains the maze. Read it into a two-dimensional array Remember you can use inputStream.get(c) to read the next character from the inputStream. This will read whitespace and non-whitespace characters Don’t forget to read the newline character at the end of each line Print the maze to the screen from your array You should include a ‘*’ in your maze to indicate where the...

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