Solved

string search:  specific word in string

Posted on 2003-11-13
10
337 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 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