ERROR WITH STRTOK

Hey everyone.Just finish reading about some string functions.I decided to do a little exercise, but I am stuck.

I want to user strtok with replace.I want to separate a word, find the word and replace it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
	string message = "John went to school to program.Why dont you join john";
			char *pnt;
			
			pnt = strtok(msg, " ");
			
			while(pnt != NULL)
			{
				
				if(pnt == "me")
				{
					msg.replace(pnt, strlen(pnt), "you");
				}
				else if(pnt == "john")
				{
					msg.replace(pnt, pnt.length(), "marry");
				}
				
			}
			
			
		}
	}


Please tell me what I am doing wrong
do not use string with strtok() (it shouldn't compile).

Better: do not use strtok() at all

to separate words you can use stringstream:

http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream
So while I was looking for a solution.I found split().When I try to use it, it says it was not declared.Is it a c++ library
so I decided to use find and replace.But Some reason, I am getting erronous results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

{
			size_t f = msg.find("john");
			size_t s = msg.find("me");
		
			if(f !=std::string::npos)
			{
			msg.replace(f,4, "marry");
			}
			if(s !=std::string::npos)
			{
			
			msg.replace(s, 2, "you");
			}
			
		}
	}


Guys.What am I doing wrong now.
you cannot use s after you replaced with f (or you recalculate s).

"marry" has 5 character while "john" only 4.

so move line 4 directly befor line 10
Topic archived. No new replies allowed.