[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

string search:  specific word in string

Posted on 2003-11-13
10
Medium Priority
?
343 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Accepted Solution

by:
n_fortynine earned 1000 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

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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 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