Solved

string search:  specific word in string

Posted on 2003-11-13
10
340 Views
Last Modified: 2010-04-02
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.



0
Comment
Question by:puckerhoop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9743210
for example:
string x = "hello world";
string::size_type pos = x.find("ello", 0);
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9743224
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
 

Author Comment

by:puckerhoop
ID: 9743302
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
n_fortynine earned 250 total points
ID: 9743586
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9743600
>>pos < str.length() - 1
should be pos < str.length() ;D
0
 

Author Comment

by:puckerhoop
ID: 9743716
what is 'D'  (decrement?)
my compiler doesn't like it... does it need to be declared?
0
 

Author Comment

by:puckerhoop
ID: 9743719
Visual C++ 6.0 (compiler)
0
 

Author Comment

by:puckerhoop
ID: 9743742
maybe:
pos < str.max_size();
??
0
 

Author Comment

by:puckerhoop
ID: 9743780
Made some slight modification to work with my compiler!
THANKS!
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9745068
puckerhoop, sorry I just got back from work. That's a smiley! :D
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question