I have a program that needs to replace all occurrences of the words "Giants" with "New York Giants". So the following line:

The Giants won a big game today, it was the first playoff win for the Giants in 7 years

Would convert to

The New York Giants won a big game today, it was the first playoff win for the New York Giants in 7 years

I'm a total newbie so I apologize for the stupid question.

Welcome aboard...if your going to be reading in the sentence from an infile, you check this and see if it helps...but you need to show what you've got to get additional help:
http://www.daniweb.com/forums/thread92572.html

>>I'm a total newbie so I apologize for the stupid question
Not a stupid question. That's a useful thing to know.

This is c++ board -- so use std::string class and its find and substr methods to find the word "Giants" and replace it with the replacement string. You need to put that in a loop until all occurances have been replaced.

Here is a good site about std::string class

The string is being inputted by a user through the kb.

getline(cin, mystring);

It's the mystring where I'll need to do the search and replace.

That's not a problem. I assume mystring is a std::string object. Yes? If not then your getline() is wrong.

So, where is your attempt to solve the problem ?

That's not a problem. I assume mystring is a std::string object. Yes? If not then your getline() is wrong.

So, where is your attempt to solve the problem ?

This is what i've got, which seems to be working:

#include <iostream>
#include <string>

using namespace std;

void replace(string &str, const string &find_what, const string &replace_with);

int main()
{
	string holdprogram;
	string sentence;
	
	getline(cin, sentence);
	
	replace(sentence, "Giants", "New York Giants");
	cout << sentence.c_str();
	
	cin >> holdprogram;
}

void replace(string &str, const string &find_what, const string &replace_with)
{
	string::size_type pos=0;
	while((pos=str.find(find_what, pos))!=string::npos)
	{
		str.erase(pos, find_what.length());
		str.insert(pos, replace_with);
		pos+=replace_with.length();
	}
}

Hummm -- from whom did you plagerize that code (rhetorical question so no need to respond) ? I assume you did because one hour is an awful short amout of time for a newbe to write it.

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.