943,965 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4272
  • C++ RSS
Aug 22nd, 2007
0

string searching

Expand Post »
can anybody tell me the coding for a program to search an element in a string exactly the same word ,like if we are having two words is and this in string then we have to search for is then it only shows is not this........
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gaggu82 is offline Offline
4 posts
since Aug 2007
Aug 22nd, 2007
0

Re: string searching

your english is very unclear. i do not understand you.

if i guess correctly. you have one word. and in that word you have to find another.

the best that i can think of is if you put the first word in a character array. and then you loop through the array. when you find the first letter of the word you are searching for you check to see if the next letter in the first word is equal to the next letter in the word you are looking for. and so on. if you find all the letter like that then you have the word. but if you dont find all the letters all, the one after the other, then only part of the word is there.

reply as to whether this is clear or not. and if this is not what you are looking for then please repeat yourself more clearly.
Reputation Points: 160
Solved Threads: 3
Posting Whiz in Training
quintoncoert is offline Offline
266 posts
since May 2007
Aug 22nd, 2007
1

Re: string searching

There might be a way in the STL to do it, but I don't know. I'd write a search function that finds every occurrence of "is" and then checks to see if the first and last letters are on a word boundary.
C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <cctype>
  3.  
  4. using namespace std;
  5.  
  6. // Check if a series of characters is on a word boundary
  7. bool isWord( string source, int start, int length ) {
  8. bool isLeftBoundary = ( start == 0
  9. || isspace( source[start - 1] )
  10. || ispunct( source[start - 1] ) );
  11.  
  12. bool isRightBoundary = ( start + length == source.length()
  13. || isspace( source[start + length] )
  14. || ispunct( source[start + length] ) );
  15.  
  16. return isLeftBoundary && isRightBoundary;
  17. }
  18.  
  19. // Find the starting index of a word in the string
  20. int findWord( string source, string word ) {
  21. int index = 0;
  22.  
  23. while ( ( index = source.find( word, index ) ) != string::npos ) {
  24. if ( isWord( source, index, word.length() ) ) {
  25. return index;
  26. }
  27.  
  28. index += word.length();
  29. }
  30.  
  31. return string::npos;
  32. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 22nd, 2007
0

Re: string searching

Click to Expand / Collapse  Quote originally posted by Hamrick ...
There might be a way in the STL to do it, but I don't know. I'd write a search function that finds every occurrence of "is" and then checks to see if the first and last letters are on a word boundary.
Yep, the STL is handy here, although your way works fine too.
CPP Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. int main()
  6. {
  7. const std::string line_in("the cat sat on the mat");
  8. const std::string match("cat");
  9. std::string::const_iterator iter =
  10. std::search(
  11. line_in.begin(),
  12. line_in.end(),
  13. match.begin(),
  14. match.end()
  15. );
  16.  
  17. if (iter != line_in.end() )
  18. std::cout << std::string(iter, line_in.end() ) << std::endl;
  19. }
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 22nd, 2007
0

Re: string searching

I ran your code, Bench, but it doesn't do what gaggu82 asked for. It finds the search string even if it's embedded in another word, like "this is a string" prints "is is a string" instead of "is a string". Am I testing it the wrong way?
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 22nd, 2007
0

Re: string searching

Click to Expand / Collapse  Quote originally posted by Hamrick ...
I ran your code, Bench, but it doesn't do what gaggu82 asked for. It finds the search string even if it's embedded in another word, like "this is a string" prints "is is a string" instead of "is a string". Am I testing it the wrong way?
No, you're not I think the original post was unclear to me (It makes more sense now - the thread starter could have done with more punctuation though - the question was bordering on cryptic as I read it)

in which case, i'd add this to the top
CPP Syntax (Toggle Plain Text)
  1. bool is_space_punct( std::string::const_iterator iter )
  2. {
  3. return isspace( *iter ) || ispunct( *iter ) ;
  4. }
and use this for bounds checking (I don't think the STL can help much with this bit)
CPP Syntax (Toggle Plain Text)
  1. bool is_word = true;
  2. if (iter != line_in.begin() ) //Don't check the beginning
  3. is_word = is_space_punct( iter-1 );
  4.  
  5. if ( iter+match.size() != line_in.end() ) //Don't check the end
  6. is_word = is_word && is_space_punct( iter+match.size() );
Now its fairly similar to yours, but with iterators rather than indices (I tend to find iterators are a bit more idiomatic for C++, probably because of the huge number of things you can do with the STL algorithms)
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 22nd, 2007
0

Re: string searching

That's really cool. Thanks for showing me another way to do it.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help me to Implement the AVLtree's remove algorithm
Next Thread in C++ Forum Timeline: T9 Predictive input and dictionary





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC