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........

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.

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.

#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;
}

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.

#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;
}

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 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

bool is_space_punct( std::string::const_iterator iter )
{
    return isspace( *iter ) || ispunct( *iter ) ;
}

and use this for bounds checking (I don't think the STL can help much with this bit)

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() );

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) :)

This question has already been answered. Start a new discussion instead.