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...
For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
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...
You are to write a program in C to do the following in a loop for...
You are to write a program in C to do the following in a loop for the KL46Z . Prompt the user for a positive integer greater than 1 and sanity-check the input. If the number is a prime number, it is to be printed on a new line in red text. If the number is evenly divisible by 7, it is to be printed on a new line in green text. If the current number is evenly divisible by...
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.
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT