Link to home
Start Free TrialLog in
Avatar of puckerhoop
puckerhoop

asked on

string search: specific word in string

Any suggestions on how to find a SPECIFIC word in a string?

ie. looking for "the"
in string:  In the end, I figured it out.



Avatar of n_fortynine
n_fortynine

for example:
string x = "hello world";
string::size_type pos = x.find("ello", 0);
notice that this will only find the FIRST occurence of "ello", to keep searching you've got to search not from 0 but from the end of the previous occurrence.
Avatar of puckerhoop

ASKER

In Unix, you can use
  grep '^he.*'
To find an occurence beginning with "he", is there such way to do this is C++?

Any way to limit it to occurences of the specific word, ONLY?  (only when it is solo or followed by punctuation).

I am going to need to replace the word... would it be better to use the string function for replace?

Tx!

ASKER CERTIFIED SOLUTION
Avatar of n_fortynine
n_fortynine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>pos < str.length() - 1
should be pos < str.length() ;D
what is 'D'  (decrement?)
my compiler doesn't like it... does it need to be declared?
Visual C++ 6.0 (compiler)
maybe:
pos < str.max_size();
??
Made some slight modification to work with my compiler!
THANKS!
puckerhoop, sorry I just got back from work. That's a smiley! :D