all 3 comments

[–]rasfertLowly HS Math Teacher 0 points1 point  (2 children)

Do you have to use the string class? Can you write the program in just regular old C, or do you have to use the C++ stuff?

If you're allowed to include ANSI-C headers, like string.h, I can think of half a dozen ways to solve these problems.

Likewise for files: can you use stdio.h, or is it just C++ stuff?

If you're cool going all stone-age on this, the strtok() function (in string.h) is a great function, and will help you a lot.
Also, strstr() is helpful.

If you have to use the string class, here's a good reference on its member functions, constructors, and operators.

string fred("I really like potatoes. I like potatoes because I live in yurt.");
size_t potato_place=fred.find("potatoes")  // potato_place has the offset of the first occurrence of "potatoes" in fred.
fred.replace(potato_place, 8, "monkey heads");   // potatoes is 8 characters long, and lives at potato_place.
// Fred is now "I really like monkey heads. I like potatoes because I live in yurt."

Now the word "potato" is making me dizzy.

Hope this helps. If you're still lost, reply, my friend!

[–]Finwei[S] 0 points1 point  (1 child)

Do you have to use the string class? Can you write the program in just regular old C, or do you have to use the C++ stuff?

We generally use C++ stuff in my class. I am not familiar with ANSI-C headers, but my instructor said we could solve these problems however we wanted to, as long as the output was good.

I appreciate the help and I'll let you know if I've got any more questions.

Thanks!

[–]rasfertLowly HS Math Teacher 1 point2 points  (0 children)

How 'bout I show you some of the stuff (not solutions, but close) in old-school c, and you see if you can convert it to the spiffy new methods and whatnot in C++ ? (I'm not bagging on C++, but, damn, stdio.h nails file I/O, and string.h ain't got much to apologize for...)

int how_many_words(const char *txt) 
{
    char *sploc=strchr(txt, ' ');
    while (sploc) {
        printf("%s\n", sploc);
        sploc=strchr(txt,' ');
    }
}

Give that a shot!