I've been assigned in my class to do this exact same problem:

http://www.chegg.com/homework-help/questions-and-answers/write-program-substitute-one-string-new-string-throughout-text-several-sets-strings-progra-q5121340

I'm wondering on how to get started.

Any help would be appreciated. I know how to use the functions for c-strings, but I'm not sure if I should place all the contents of the paragraph into one char array or if I should look at each piece individually, swap, and then print or should I use a two dimensional array to place each word in a block?

Thank you.

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.  Note: you need to do the C-String program first.
void read()
{
    int const SIZE = 100;
    char s1[SIZE][SIZE];
    int i = 0, k = 0, num = 0;
    char temp;

    ifstream myfile ("paragraphwordsub.txt");
    myfile >> std::noskipws;

    if (myfile.is_open())
    {
        myfile >> temp;
        while (temp!= '$')
        {
            if (temp != ' ')
            {
                s1[i][k] = temp;
                myfile >> temp;
                k++;
            }
            else
            {
                s1[i][k+1] = '/';
                k=0;
                myfile >> temp;
                if (temp == ' ')
                {
                    myfile >> temp;
                }
                i++;
                num++;
            }
        }
    }

    else cout << "Unable to open file";

    for(i=0;i<num;i++)
    {
        k = 0;
        while(s1[i][k] != '/')
        {
            cout << s1[i][k];
            k++;
        }
    }

    cout << endl << endl << s1[2][0];

    return;
}

I decided to do a two dimensional array, but I'm not doing so well.

Tommy  EveryBody
programs  job
Samson  Anybody
were  was
Sam  Somebody
such  it
Nicholsonnders  Nobody
- .
$
                               Whose programs is such?
    There were important programs to be done and Tommy were asked to do such-  Tommy were sure Sam would do such-  Samson could have done 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 done-

This is the input file I'm reading in from. I have to take the words above the $ and use them to replace the words in the paragraph.

I have to make a program using c-strings, and one with C++ strings.

How would I be able to read all the data from the input file above and then replace it in the subsequent paragraph using only c-strings?

You started with the right approach, just that you are reading the data wrong. Read the data an entire word at a time. Note that you will need a 3 dim array instead of 2d array.

void read()
{
    int const SIZE = 100;
    char s1[SIZE][SIZE][40] = {0};
    int i = 0, k = 0, num = 0;
    char temp[SIZE];

    ifstream myfile ("paragraphwordsub.txt");

    if (myfile.is_open())
    {
        num = 0;
        whyile(myfile >> s1[num][0] >> s1[num][1] )
        {
           if( s1[num[1][0] == '$')
              break;
           num++;
        }
    }
    else
    {
        cout << "Unable to open file";
        return 1;
    }

    return 0;
}

Edited 2 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.