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 replacment

Sample Output:

Welcome to the find and replace utility!

Input filename? AllOfMe.txt

Find what? Love

Replace with? Hate

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 all of me
Hates all of you
Hate your curves and all your edges
All your perfect imperfections

Solutions

Expert Solution

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

int main(){
        
        char inputFile[100];
        char outputFile[100];
        string inpFileText;
        string outFileText;
        
        // reading filename
        cout << "Enter Input filename ? ";
        cin >> inputFile;
        ifstream file1(inputFile);
        
        // checking file exists or not
        if(file1.fail()){
                cout << "File not exist" <<endl;
                exit(0);
        }
        
        // read all text from file
        string text;
        string fulltext="";
        while (getline (file1, text)) {
                fulltext = fulltext + text + "\n";
        }
        
        // inpFileText contains text of file of inputFile
        inpFileText = fulltext;
        
        string find, replace;
        
        
        // reading find and replace strings
        cout << "Find What ? ";
        cin >> find;
        cout << "Replace with ? ";
        cin >> replace;
        cout << "Output filename ? ";
        cin >> outputFile;
        int index;
        
        
        // replacing find string with replace string
        while((index = fulltext.find(find)) != string::npos) { 
                fulltext.replace(index, replace.length(), replace);
        }
        
        // storing replaced string to outFileText
        outFileText = fulltext;
        cout <<endl;
        
        // writing outFileText string to outputFile
        ofstream file2(outputFile);
        file2 << outFileText;
        
        
        // printing input file text
        cout << "------------Input File Text-----------" <<endl;
        cout << inpFileText <<endl;
        
        // printing output file text
        cout << "-----------Output File Text-----------" <<endl;
        cout << outFileText <<endl;
        
        
        return 1;
}

Output:


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