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...
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...
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...
For this computer assignment, you are to write and implement an interactive C++ program to find...
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT