string searching

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2007
Posts: 4
Reputation: gaggu82 is an unknown quantity at this point 
Solved Threads: 0
gaggu82's Avatar
gaggu82 gaggu82 is offline Offline
Newbie Poster

string searching

 
0
  #1
Aug 22nd, 2007
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........
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 266
Reputation: quintoncoert is an unknown quantity at this point 
Solved Threads: 3
quintoncoert quintoncoert is offline Offline
Posting Whiz in Training

Re: string searching

 
0
  #2
Aug 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: string searching

 
1
  #3
Aug 22nd, 2007
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.
  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. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 503
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 50
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: string searching

 
0
  #4
Aug 22nd, 2007
Originally Posted by Hamrick View Post
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.
  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. }
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: string searching

 
0
  #5
Aug 22nd, 2007
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?
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 503
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 50
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: string searching

 
0
  #6
Aug 22nd, 2007
Originally Posted by Hamrick View Post
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
  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)
  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)
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: string searching

 
0
  #7
Aug 22nd, 2007
That's really cool. Thanks for showing me another way to do it.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC