Solved

string search:  specific word in string

Posted on 2003-11-13
10
334 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now