Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2007, 8:19 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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()
{
	string text = "A rose is just a rose is a rose.";
	string word = "rose";
	string replacement = "thorn";

	int start = text.find(word);

	cout << start << endl;

	while (start != string::npos)
	{
		text.replace(start, word.length(), replacement);
		start = text.find(word);
		cout << start << endl;
    }

	return 0;
}

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).
357mag is offline   Reply With Quote
Old Jun 14th, 2007, 8:25 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Frequency Regular Expression hoffmandirt C# 4 Feb 27th, 2006 8:21 PM
How to determine if it's a word aznluvsmc C 10 Aug 22nd, 2005 11:30 AM
Help with sorting and counting? mmmm_strawberries C++ 8 Apr 3rd, 2005 6:47 PM
crack these questions if u can!!! shagan C++ 18 Apr 3rd, 2005 6:47 AM
how to implemen the Find funtion in the text editor to locate a word in a document??? allen1984us C++ 4 Mar 8th, 2005 10:32 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC