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.



puckerhoopAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

n_fortynineCommented:
for example:
string x = "hello world";
string::size_type pos = x.find("ello", 0);
0
n_fortynineCommented:
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.
0
puckerhoopAuthor Commented:
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!

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

n_fortynineCommented:
Yes. For example:
#include <string>
#include <iostream>
using namespace std;

int main() {
   string str = "hello world hehehe";
   string term = "he";
   string::size_type pos = 0, occur = 0;
   while(occur != string::npos && pos < str.length() - 1) {
      occur = str.find(term, pos);
      if(occur != string::npos) {
         str.replace(occur, term.length(), "she");
         cout << occur << endl;
         pos = occur + term.length();
      }
      else pos = occur + 1;
   }
   cout << str << endl;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
n_fortynineCommented:
>>pos < str.length() - 1
should be pos < str.length() ;D
0
puckerhoopAuthor Commented:
what is 'D'  (decrement?)
my compiler doesn't like it... does it need to be declared?
0
puckerhoopAuthor Commented:
Visual C++ 6.0 (compiler)
0
puckerhoopAuthor Commented:
maybe:
pos < str.max_size();
??
0
puckerhoopAuthor Commented:
Made some slight modification to work with my compiler!
THANKS!
0
n_fortynineCommented:
puckerhoop, sorry I just got back from work. That's a smiley! :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.