QuestionQuestion

Transcribed TextTranscribed Text

Question 1 of 1: File Handling: [100 marks] Write a C++ program that will swap the words aesar and Brutus in the file "source file.txt". You will need to load the file "source file.txt" and finally write the updated contents with the replaced words to the file "target file.txt" Upon successful execution all occurrences of Caesar become Brutus and Brutus becomes Caesar You can use functions from the estring library. Please properly comment your code before submission. Expected operation : source file.txt Contents Friends, Romans, countrymen, lend me your ears; I come to bury ae not to praise him. The evil that men do lives after them; The good is oft interred with their bones; So, let it be with Caesar. The noble Brutu: Hath told you Caesar was ambitious: If it were so, it was a grievous fault, And grievously hath Caesar answer' it. Here, under leave of Brutus and the rest- For Brutu: is an honorable man; So are they all, all honorable men- Come I to speak in 's funeral. He was my friend, faithful and just to me: But Brutu: says he was ambitious; And Brutus is an honorable man. He hath brought many captives home to Rome Whose ransoms did the general coffers fill: Did this in seem ambitious? When that the poor have cried, Caesar hath wept: Ambition should be made of sterner stuff: Yet Brutus says he was ambitious; And Brutus is an honorable man. You all did see that on the Lupercal I thrice presented him a kingly crown, Which he did thrice refuse: was this ambition? Yet Brutus says he was ambitious; And, sure, he is an honorable man. I speak not to disprove what Brutus spoke, But here I am to speak what I do know. You all did love him once, not without cause: What cause withholds you then, to mourn for him? O judgment! thou art fled to brutish beasts, And men have lost their reason. Bear with me; My heart is in the coffin there with Caesar And I must pause till it come back to me. target file.txt Contents Page 1 of 2 Friends, Romans, countrymen, lend me your ears; I come to bury not to praise him. The evil that men do lives after them; The good is oft interred with their bones; So, let it be with Brutus The noble aesar Hath told you Brutus was ambitious: If it were so, it was a grievous fault, And grievously hath Brutus answer'd it. Here, under leave of Caesar and the rest- For Caesar is an honorable man; So are they all, all honorable men- Come I to speak in Brutu 's funeral. He was my friend, faithful and just to me: But aesar says he was ambitious; And Caesar is an honorable man. He hath brought many captives home to Rome Whose ransoms did the general coffers fill: Did this in Brutus seem ambitious? When that the poor have cried, Brutus hath wept: Ambition should be made of sterner stuff: Yet Caesar says he was ambitious; And Caesar is an honorable man. You all did see that on the Lupercal I thrice presented him a kingly crown, Which he did thrice refuse: was this ambition? Yet says he was ambitious; And, sure, he is an honorable man. I speak not to disprove what ae spoke, But here I am to speak what I do know. You all did love him once, not without cause: What cause withholds you then, to mourn for him? O judgment! thou art fled to brutish beasts, And men have lost their reason. Bear with me; My heart is in the coffin there with Brutu. And I must pause till it come back to me. Hint You may want to read the contents of the file one word at a time. Page 2 of 2

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include<iostream>
#include<fstream>
#include<sstream>
#include<cstring>


using namespace std;

int main(){

    // make an object of input and output stream
    ifstream inputFile;
    ofstream outputFile;

    // open source and target files
    inputFile.open("source_file.txt");
    outputFile.open("target_file.txt");

    // string variable to store line
    string line;

    // string variable to take a word from a line
    string word;

    // in order to keep file format(keep "\n" char), we read file line by line
    while(getline(inputFile, line)){

       // make an object of input string stream, just to read line word by word
       // as a parameter of istringstream constructor, we set line that we read
       istringstream iss(line);

       // redaing a line word by word
       while(iss >> word){...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$45.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats