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

<snip>

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.

Recommended Answers

All 5 Replies

I don't think some of that came out right -- what is �$� ?

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.

why read it one character at a time. You can read it a whole word at a time and save yourself a lot of grief.

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.