reading two strings and checking if string one is in string two

Hi can anyone help? I'm reading in strings from a file and calling a function to determine if string first is in string second. If it is then the function will remove string one from string two and return true or false to main. Here is my code:

//Function to determine if the first word of the original string is found
//in the second string for example if 'sage' is found in 'message' main will remove sage from message and message will now be 'mes'
bool snipsame(string first, string second)
{
string::size_type pos;
int len;

len=first.length();
pos=second.find(first,0);
if(pos!=first.npos){
second.erase(pos,len);
return true;
}
else
return false;
}
Last edited on
I have this same problem!

Sorry I can't do nothing to help, but I hope someone can cause I need to know this too
hi II15X,

I made string second a reference parameter and it worked. Hope this helps
@tyky9808
Your function works but it needs the second argument of the function to by passed by reference if you want it to affect the string passed: bool snipsame(string first, string &second) (notice the '&' )
http://www.cplusplus.com/doc/tutorial/functions2.html
Last edited on
Topic archived. No new replies allowed.