Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string search:  specific word in string

Posted on 2003-11-13
10
Medium Priority
?
342 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
Industry Leaders: 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

618 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