| | |
string searching
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 266
Reputation:
Solved Threads: 3
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.
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.
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)
#include <string> #include <cctype> using namespace std; // Check if a series of characters is on a word boundary bool isWord( string source, int start, int length ) { bool isLeftBoundary = ( start == 0 || isspace( source[start - 1] ) || ispunct( source[start - 1] ) ); bool isRightBoundary = ( start + length == source.length() || isspace( source[start + length] ) || ispunct( source[start + length] ) ); return isLeftBoundary && isRightBoundary; } // Find the starting index of a word in the string int findWord( string source, string word ) { int index = 0; while ( ( index = source.find( word, index ) ) != string::npos ) { if ( isWord( source, index, word.length() ) ) { return index; } index += word.length(); } return string::npos; }
The truth does not change according to our ability to stomach it.
•
•
•
•
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.
CPP Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <algorithm> int main() { const std::string line_in("the cat sat on the mat"); const std::string match("cat"); std::string::const_iterator iter = std::search( line_in.begin(), line_in.end(), match.begin(), match.end() ); if (iter != line_in.end() ) std::cout << std::string(iter, line_in.end() ) << std::endl; }
¿umop apisdn upside down? 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.
•
•
•
•
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?
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)
bool is_space_punct( std::string::const_iterator iter ) { return isspace( *iter ) || ispunct( *iter ) ; }
CPP Syntax (Toggle Plain Text)
bool is_word = true; if (iter != line_in.begin() ) //Don't check the beginning is_word = is_space_punct( iter-1 ); if ( iter+match.size() != line_in.end() ) //Don't check the end is_word = is_word && is_space_punct( iter+match.size() );
¿umop apisdn upside down? ![]() |
Similar Threads
- strstr (C++)
- Psuedocode (C++)
- stupid string replace! (c++) (C++)
- Searching for a string in an access database (Visual Basic 4 / 5 / 6)
- Getting a substring substring, but without 2 strings (C++)
- creating our own C++ strcat() function, code reqd (C++)
- substring problem (Java)
- want to write latin translator what language should i choose (Computer Science)
- Searching in documents... (C++)
Other Threads in the C++ Forum
- Previous Thread: Help me to Implement the AVLtree's remove algorithm
- Next Thread: T9 Predictive input and dictionary
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





