Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 318 Program Assignment String Substitution You are to write a program that wi

ID: 3761479 • Letter: C

Question

CS 318

Program Assignment

String Substitution

You are to write a program that will substitute one string for a new string throughout the text. There will be several sets of strings for this program assignment.   The input file is ‘paragraphwordsub.txt ‘.   The first word on a line will be replaced by the second word listed on the same input line. There are several pairs of words for substitutes. The flag to indicate the end of the word pairs is a ‘$’. Following the ‘$’ (next line) is the text that will be used in the word substitution.

You are to write the word substitution program twice. Write one program with using only C-String data and functions.   Then write the same program again using only C++ String Class data and functions.   Turn in the original text and the new text for each program.  

Here is the input file could not insert a text file. It is all on one line.

Tommy EveryBody
programs job
fixed fixed
Samson Anybody
were was
Sam Somebody
such it
Nicholsonnders Nobody
- .
$
Whose programs is such?
There were important programs to be fixed and Tommy were asked to do such- Tommy were sure Sam would do such- Samson could have fixed such, but Nicholsonnders did such- Sam got angry about that, because such were Tommy's programs- Tommy thought Samson could do such but Nicholsonnders realized that Tommy wouldn't do such- It ended up that Tommy blamed Sam when Nicholsonnders did what Samson could have fixed-

Explanation / Answer

Answer:

Program1:

/***Read from file using c-string data and function****/

#include<stdio.h>

#include <string.h>

#include <conio.h>

//main method

int main( )

{

//DECLARING VARIABLES

char rStr[500];

int k2,k1;

int n_Vl;

char rStr1[100][100],rStr2[100][100];

FILE *iFR;

char In_File_Name[10];

//GET FILENAME FROM USER

printf(" ENTER FILE NAME:");

scanf("%s",&In_File_Name);

iFR = fopen(In_File_Name, "r");

k1=0;

//READ FROM FILE FOR WORD AND REPLACE WORD

while(!feof(iFR))

{

fscanf(iFR,"%s",&rStr1[k1]);

if(!strcmp(rStr1[k1],"$"))

break;

fscanf(iFR,"%s",&rStr2[k1]);

k1++;

}

n_Vl=k1;

//READ THE TEXT

while(!feof(iFR))

{

fscanf(iFR,"%s",&rStr);

//LOOP TO REPLACE THE WORD IN THE TEXT

for(k2=0;k2<n_Vl;k2++)

{

if(!strcmp(rStr,rStr1[k2]))

{

strcpy(rStr,rStr2[k2]);

break;

}

}

//PRINT THE TEXT

printf("%s",rStr);

printf(" ");

}

//CLOSE THE FILE

fclose(iFR);

getch();

return 0;

}

Program2:

/***Read from file using c++ string class****/

#include<iostream>

#include <string>

#include <fstream>

using namespace std;

//METHOD TO REPLACE STRING

std::string changeCon(std::string realSt, const std::string& oldSt, const std::string& newst) {

    size_t p1 = 0;

    while ((p1 = realSt.find(oldSt, p1)) != std::string::npos) {

         realSt.replace(p1, oldSt.length(), newst);

         p1 += newst.length();

    }

    return realSt;

}

//MAIN METHOD

int main( )

{

     int k2;

     string rStr;

int nVl;

int k1;

string rStr1[100];

string rStr2 [100];

string In_File_Name;

/GET FILENAME FROM USER

cout<<" ENTER FILE NAME";

cin>>In_File_Name;

//OPEN THE FILE

ifstream in_File12(In_File_Name);

k1=0;

if (in_File12.is_open())

{

while(in_File12>>rStr1[k1])

{

if((rStr1[k1].compare("$"))==0)

break;

in_File12>>rStr2[k1];

k1++;

}

nVl=k1;

while(in_File12>>rStr)

{

for(k2=0;k2<nVl;k2++)

{

     rStr= changeCon(rStr,rStr1[k1],rStr2[k1]);

}

}

}

//DISPLAY THE TEXT WITH REPLACED STRING

cout<<rStr;

in_File12.close();

return 0;

}