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

You are to write a c++ program that will substitute one string for a new string

ID: 3765699 • Letter: Y

Question

You are to write a c++ 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. Note: you need to do the C-String program first.

txt file....

Explanation / Answer

Completed question 1 = to replace using c style strings
#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;


char* replace(char *para, char *serword, char *repword)
{
// This could be improved (I was lazy and made an array twice the size)
    char* repstring = new char[strlen(para) * 2];

    int pos = 0;
    for (int i = 0; *(para + i); ++i)
    {
        if (*(para + i) == *(serword))
        {
            // Got a match now check if the two are the same
            bool same = true; // Assume they are the same
            for (int j = 1, k = i + 1; *(serword + j) && *(para + k); ++j, ++k)
            {
                if (*(serword + j) != *(para + k))
                {
                    same = false;
                    break;
                }
            }
            if (same)
            {
                // They are the same now copy to new array
                for (int j = 0; *(repword + j); ++j)
                {
                    repstring[pos++] = *(repword + j);
                }
                i += strlen(serword) - 1;
                continue;
            }
        }
        repstring[pos++] = *(para + i);
    }
    repstring[pos] = '';
    return repstring;
}


int main()
{

    char* para = "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 ab";

    cout << " Original para: " <<para <<endl;
       ifstream dataIn;
        dataIn.open("D://ravi/Cheg//paragraphwordsub.txt");
        char word[100], repword[100];
        int paralen = strlen(para);

        while ( dataIn ) {
            dataIn >> word;
            if(strcmp(word,"$") == 0)
                break;
            dataIn >> repword;

            //cout << word << " --"<<repword <<endl;
            //Replace the word
            para = replace(para, word, repword);
        }

        cout << " Replaced para: " <<para <<endl;
}

--------------output----------------------

Original para:
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 ab
Replaced para:
Whose job is it?
There was important job to be fixed and EveryBody was asked to do it- EveryBody was sure Somebody would do it- Anybody could have fixed it, but Nobody did it- Somebody got angry ab

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote