357mag
06-06-2007, 01:05 AM
I'm working on this cool program that replaces a word in a string with another word. It's out of one of my C++ books. Here is the code:
int main()
{
string text = "A rose is a rose is a rose.";
string word = "rose";
string replacement = "horse";
int start = 0;
start = text.find(word);
while (start != string::npos)
{
text.replace(start, word.length(), replacement);
start = text.find(word, start + replacement.length());
}
cout << text << endl;
return 0;
}
The line:
start = text.find(word)
I know basically what that does. It justs finds the starting index of the word and assigns it to the start variable.
The line:
while (start != string::npos)
I guess what that does is tell the compiler to keep going through the string until the word "rose" is no longer found. Okay.
The line:
text.replace(start, word.length(), replacement)
This tells the compiler to remove 4 characters("rose") starting at index position 2(where "rose" is first found) and replace it with "horse." Okay.
But I do not understand this line at all:
start = text.find(word, start + replacement.length())
I did a test and I commented out some lines of code and ran the program and the compiler said the next index position of word "rose" was found at index position 12. Okay. So if the compiler has already moved to index position 12 and has already found the next occurrence of "rose" at index position 12, what is the purpose of start + replacement.length()?
I guess it has something to do with the likelyhood that the replacement word is a different length than the original word. I get that. But if text.find(word) has already returned the new index position of the next occurrence of "rose", what happens immediately after that?
I just don't understand that line no matter how I try. I'm close, but I need some help.
int main()
{
string text = "A rose is a rose is a rose.";
string word = "rose";
string replacement = "horse";
int start = 0;
start = text.find(word);
while (start != string::npos)
{
text.replace(start, word.length(), replacement);
start = text.find(word, start + replacement.length());
}
cout << text << endl;
return 0;
}
The line:
start = text.find(word)
I know basically what that does. It justs finds the starting index of the word and assigns it to the start variable.
The line:
while (start != string::npos)
I guess what that does is tell the compiler to keep going through the string until the word "rose" is no longer found. Okay.
The line:
text.replace(start, word.length(), replacement)
This tells the compiler to remove 4 characters("rose") starting at index position 2(where "rose" is first found) and replace it with "horse." Okay.
But I do not understand this line at all:
start = text.find(word, start + replacement.length())
I did a test and I commented out some lines of code and ran the program and the compiler said the next index position of word "rose" was found at index position 12. Okay. So if the compiler has already moved to index position 12 and has already found the next occurrence of "rose" at index position 12, what is the purpose of start + replacement.length()?
I guess it has something to do with the likelyhood that the replacement word is a different length than the original word. I get that. But if text.find(word) has already returned the new index position of the next occurrence of "rose", what happens immediately after that?
I just don't understand that line no matter how I try. I'm close, but I need some help.