Question

In C++ In this homework, you will be tasked with creating functions to manipulate strings that...

In C++

In this homework, you will be tasked with creating functions to manipulate strings that come from files and then outputting the strings to another file. All of these functions are housed together in a menu. The user will be asked to input a file name and then their menu selection.

The menu is this:

  1. Get rid of white space
  2. Print amount of characters in the file
  3. Print amount of words in the file
  4. Replace all vowels with 3's
  5. Print the file lines in reverse order

Input will look like this: "input1.txt 2". You can assume there will be no numbers in the input files. Make sure that you have variables to store both the input file name and the integer selection.

Menu Items Explained

  1. Delete all spaces between words and print that to the new file, line-by-line
  2. Count the number of characters in the whole file, not including spaces, and print that to the new file
  3. Count the number of words in the whole file and print that to the new file
  4. Replace all vowels with 3's and print that to the new file, line-by-line
  5. Given the order of the file lines (Ex: 1 2 3), print to the new file the reverse (Ex: 3 2 1)

Each operation must be performed by a separate function. Submissions will receive a 10 points deduction for every function missing.

There should be nothing printed to the console, all input is taken from one file and outputted to another file! The output file should be named output.txt!

You will be using your own IDE for this homework instead of the zybook IDE. At the bottom of the page, there is an option to upload files. Please upload your .cpp file(s) to be graded.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code to be Copied:

Stringoperations.cpp

//include required header files
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;

//function prototypes
void removeWhiteSpace(ifstream &, ofstream&);
void replaceVowel(ifstream &, ofstream &);
int countWords(ifstream & infile);
int countCharcters(ifstream &);
void reverse(ifstream &, ofstream &);
void mainMenu();

//main method
int main()
{
   //declare variables
   string inputfile;
   int choice, totalChar, totalWords;
   ifstream infile;
   ofstream outputfile;

   //prompt and read the file name from the user
   cout << "Enter the name of the file: ";
   cin >> inputfile;
   //open the output file
   outputfile.open("Output.txt");
   //open the input file
   infile.open(inputfile);

   //if the file unable to open
   //then program will exit
   if (!infile)
   {
       cout << "Unable to opent the file... File not found.\n";
       return 0;
   }
   //call the method to display the menu
   mainMenu();
   //read the choice
   cin >> choice;
   //using switch case for the different choices
   switch (choice)
   {

   //case 1 to remove white spaces
   case 1:
       removeWhiteSpace(infile, outputfile);
       break;

   //case 2 to count number of characters in the file
   case 2:
       totalChar = countCharcters(infile);
       outputfile << "Total number of characters in the "
           << inputfile << " are :" << totalChar << endl;
       break;

   //case 3 to count number of characters in the file
   case 3:
       totalWords = countWords(infile);
       outputfile << "Total number of words in the "
           << inputfile << " are :" << totalWords << endl;
       break;

   //case 4 replace the vowel as "sss" in the content
   case 4:
       replaceVowel(infile, outputfile);
       break;

   //case 5 reverse the order of lines in the file
   case 5:
       reverse(infile, outputfile);
       break;
   default:
       cout << "Invalid choice!!!" << endl;
       break;
   }
   //close the input file
   infile.close();
   //close the output file
   outputfile.close();
   return 0;
}
//implement method to print the menu
void mainMenu()
{
   cout << "1. Get rid of white space." << endl;
   cout << "2. Print amount of characters in file." << endl;
   cout << "3. Print amount of words in file." << endl;
   cout << "4. Replce all vowels with 3's." << endl;
   cout << "5. Print the file lines in reverse order." << endl;
   cout << "\Enter the choice: ";
}


//Implement function that will remove the
//white space and write to file
void removeWhiteSpace(ifstream &infile, ofstream &out)
{
   //decalre variable
   string line;
   //use while loop to read the line
   while (getline(infile, line))
   {
       line.erase(remove(line.begin(), line.end(), ' '), line.end());
       out << line << endl;
   }
}

//implement the function that will count the total
//number of characters in the file and return it
int countCharcters(ifstream & infile)
{
   //declare the input line
   string line;
   int count = 0;
   //use while loop to read the file
   while (getline(infile, line))
   {
       //use for-loop to read each character
       for (int i = 0; i < line.size(); i++)
       {
           //if space is found then
           //increment the count variable
           if (line[i] != ' ')
           {
               count++;
           }
       }
   }
   //return the count value
   return count;
}

//implement function that will count the total
//words in the file and return it
int countWords(ifstream & infile)
{
   //declare variable
   string line;
   int count = 0;


   //use while loop to read the input line
   while (getline(infile, line))
   {
       //to read each word
       for (int i = 0; i < line.size(); i++)
       {
           //if space or tabspace found then increment word count
           if (line[i] == ' ' || line[i]=='   ')
           {
               count++;
           }
       }
       count++;
   }
   //return the count variable
   return count;
}

//Implement the function to replace the vowels with the "3"
void replaceVowel(ifstream &infile, ofstream &out)
{
   //decalre variables
   string input, newStr = "";
   //use while loop to prompt and read the
   while (getline(infile, input))
   {
       //initialize variable
       newStr = "";
       //use for-lop to replace vowel character in the line
       for (char& c : input)
       {
           if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
               || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
           {
               newStr += "3";
           }
           else
           {
               newStr += c;
           }
       }
       //write the output to the file
       out << newStr << endl;
   }
}

//Implement function that will reverse the
//lines order in the file
void reverse(ifstream &infile, ofstream &out)
{
   //decalre variable
   string str;
   //decalre string array
   string temp[50];
   //declare variable of type integer
   int i = 0,count = 0;
   //use while loop to read the
   //input lines in the file
   while (getline(infile, str))
   {
       //store each line in the array
       temp[i] = str;
       //increment i value
       i++;
       //increment count
       count++;
   }
   //use for-loop to write the
   //lines in reverse order
   for (i = count - 1; i >= 0; i--)
   {
       out << temp[i];
       out << endl;
   }
}

Add a comment
Know the answer?
Add Answer to:
In C++ In this homework, you will be tasked with creating functions to manipulate strings that...
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
  • Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...

    Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words (strings of characters separated by blanks or new lines), and characters in a file (not including blank spaces between words). Write your own version of this program. You will need to create a text file with a number of lines/sentences. The program should accept a filename (of your text file) from the user and then print three numbers: The count of lines/sentences The count...

  • PA #1: Word Counter Tabulating basic document statistics is an interesting exercise that leverages your knowledge...

    PA #1: Word Counter Tabulating basic document statistics is an interesting exercise that leverages your knowledge of strings, files, loops, and arrays. In this homework, you must write a C++ program that asks the user for an input and output file. For each line in the input file, write a modified line containing a line number to the output file. Additionally, calculate the number of paragraphs, lines, words, and characters. Write the summary information to the bottom of the output...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • 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,...

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. 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 opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with 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