Question

In: Computer Science

You will develop a program in C++ that will do a "find and replace" on an...

You will develop a program in C++ that will do a "find and replace" on an input file and write out the updated file with a new name.

It will prompt your user for four inputs:

The name of the input file.

The characters to find in the input file.

The replacement (substitute) characters.

The name of the output file.

It will perform the replacement.

Note1: all instances must be replaced not just the first one on a line.

Note2: this is a case sensitive replacement

And it should allow the string to find or the replacement to contain blanks.

It must have at least 2 functions in addition to main. Note: functions should be after main with their prototypes before main.

And it should be checking for output file failure

Sample Output:

Welcome to the find and replace utility!

Input filename? AllOfMe.txt

Find what? all

Replace with? most

Output filename? AOM_out

Input File:

Cause all of me
Loves all of you
Love your curves and all your edges
All your perfect imperfections

Output File

Cause most of me
Loves most of you
Love your curves and all your edges
All your perfect imperfections

Solutions

Expert Solution

#include <bits/stdc++.h> 
#include <string>
using namespace std; 
  

int getStringLength(string sourceString);
string replaceString(string sourceString,string findWhat, string replaceWith);  
// Driver code 
int main() 
{ 
        string inputFileName,findWhat,replaceWith,outputFileName,line;
        cout<<"\n Welcome to the find and replace utility";
        cout<<"\n Input File Name ?";
        cin>>inputFileName;
        cout<<"\nFind What?";
        cin>>findWhat;
        cout<<"\n Replace With?";
        cin>>replaceWith;
        cout<<"\n Output File Name?";
        cin>>outputFileName;

        ifstream myfile (inputFileName+".txt");
          if (myfile.is_open())
          {
        ofstream MyFile(outputFileName+".txt");
            while ( getline (myfile,line) )
            {
                string finalString = replaceString(line, findWhat,replaceWith);
                MyFile<<finalString<<"\n";
            }
            myfile.close();
            MyFile.close();
          } else {
        cout<<"Could not open the source file";   
        }

} 
string replaceString(string sourceString,string findWhat, string replaceWith)
{
        string str = sourceString;
        size_t index = 0;
        while (true) {
     /* Locate the substring to replace. */
     index = str.find(findWhat, index);
     if (index == std::string::npos) break;

     /* Make the replacement. */
     str.replace(index, getStringLength(findWhat), replaceWith);

     /* Advance index forward so the next iteration doesn't pick it up as well. */
     index += getStringLength(findWhat);
}
return str;
}

int getStringLength(string sourceString) {
        int length =0 ; 

        length = sourceString.length();

        return length;
}

Related Solutions

You will develop a program in C++ that will do a "find and replace" on an...
You will develop a program in C++ that will do a "find and replace" on an input file and write out the updated file with a new name. It will prompt your user for four inputs: The name of the input file. The characters to find in the input file. The replacement (substitute) characters. The name of the output file. It will perform the replacement. Note1: all instances must be replaced not just the first one on a line. Note2:...
Develop and write a program in c/c++ that will find the first 100 primes (starting at...
Develop and write a program in c/c++ that will find the first 100 primes (starting at 1) and write them to a file named "primes.dat".
C++ program: ABC Co wants you to develop a C++ program to calculate the total invoice...
C++ program: ABC Co wants you to develop a C++ program to calculate the total invoice based on the user inputs item price and the quantity of the item purchased, taking into consideration the discount given for each category as follow: Less than 10 Item: No discount Between 10 and 20 items: 10 % off the item price More than 20 items: 20% off the item price. Your Program should display, the total before discount, how much discount and the...
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Develop a client /server talk application in C or C++. You should run your server program...
Develop a client /server talk application in C or C++. You should run your server program first, and then open another window if you use loopback to connect the local host again, or you may go to another machine to run the client program that will connect to the server program. In this case you must find the server’s IP address first, and then connect to it. If you use loopback, your procedure may look like: ➢ Server Server is...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a)...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a) Create six nodes of integers such as n0, n1, n2, n3, n4, n5 (n0 is the head) b) Assign data to each nodes such as n1->data = 2; n2->data = 5, n3->data = 3, n4->data = 10, n5->data = 1. c) Make n0 ->next to point to n1, and n0->prev to point to NULL 2.) Print out the content of list you have created...
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will...
PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will allow you to keep track of a set of employees. In reviewing your employee list, you notice that your employees fall into two categories: Salaried and Hourly. The following table shows the information that you keep in your employee list for each type of employee. Field Type Salaried Hourly id int Yes Yes name String Yes Yes title String Yes No position String No...
C or C++ program Create a list of 5 things-to-do when you are bored (in the...
C or C++ program Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT