![]() |
Replacing a word with another word
This probably is going to be just one of several questions I'm going to ask regarding this program. The program has a string of text, and then replaces a word with another word. But to start with all I'm trying to do here is just output the index positions of the word "rose". Here is the program:
:
int main()When I run the program it outputs the following: 2 18 29 -1 But I was expecting it to output the following: 2 17 27 Because the index position of the first occurrence of the word "rose" is found at index position 2. Then if you keep counting, the second occurrence of the word "rose" is found at index postion 17. And the final occurrence of the word "rose" is found at index position 27. And I don't know what the -1 is for either(maybe that is the illegal character position that is returned when the while test condition eventually fails). |
Surely you realize that if the word, "rose", is replaced by the word, "thorn", then every subsequent character moves to a position one greater than before. Your next find operation works on the NEW text, not the old text.
Incidentally, have you checked on the value of string::npos in your environment? |
| All times are GMT -5. The time now is 2:35 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC