views:

223

answers:

3

how can i find the first word in my sentence having 'w' character.This character can be present anywhere in my word.example of sentence "Hi xyzwy! what are you doing here?" So the result should be "xyzwy".

+3  A: 

Go over your string starting from the first character until the last. Check if you encounter 'w'. If yes, backtrack until you hit a word separator (a space for example) or you reach the beginning of your string, then print all characters until you encounter another word separator (or the end of the string).

string Str;
getline(cin, Str);

for ( int i = 0; i < Str.length(); ++i )
  if ( Str[i] == 'w' )
  {
    // backtrack and print
    break;
  }

Or use the find method of the string class to do the searching for you, then you just need to identify the word.

IVlad
+1  A: 
boost::optional<std::string>
find_word_with(std::string const& haystack, std::string const& needle) {
  std::istringstream ss (haystack);
  for (std::string word; ss >> word;) {
    if (word.find(needle) != word.npos) {
      return boost::optional<std::string>(word);
    }
  }
  return boost::optional<std::string>();
}

std::string const whitespace = " \t\r\n\v\f";
boost::optional<std::string>
find_word_with2(std::string const& haystack, std::string const& needle) {
  typedef std::string::size_type Pos;

  Pos found = haystack.find(needle);
  if (found == haystack.npos) {
    return boost::optional<std::string>();
  }

  Pos start = haystack.find_last_of(whitespace, found);
  if (start == haystack.npos) start = 0;
  else ++start;

  Pos end = haystack.find_first_of(whitespace, found+1);
  if (end == haystack.npos) end = haystack.length();

  return boost::optional<std::string>(haystack.substr(start, end - start));
}

Both of these only separate words on whitespace (I missed that you wanted "xyzwy" instead of "xyzwy!" at first), but you could modify them to ignore punctuation. The first isn't very amenable to that, but the second could be easily modified to use find_first/last_not_of with the equivalent of regex \w ("ABC..abc..012.._") instead of checking for whitespace.

Note that the second, using the hardcoded whitespace variable, is not locale-aware as the stream solution is (which uses the last set global locale), but it may be just what you want.

int main() {
  {
    boost::optional<std::string> r =
      find_word_with("Hi xyzwy! what are you doing here?", "w");
    if (!r) std::cout << "not found\n";
    else std::cout << "found: " << *r << '\n';
  }
  {
    boost::optional<std::string> r =
      find_word_with2("Hi xyzwy! what are you doing here?", "w");
    if (!r) std::cout << "not found\n";
    else std::cout << "found: " << *r << '\n';
  }
  return 0;
}
Roger Pate
+1  A: 

If you really need regex, you can use

\w*w\w*

For example:

#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace boost;
using namespace std;

int main () {
    string s;
    getline(cin, s);
    match_results<string::const_iterator> m;
    if (regex_search(s, m, regex("\\w*w\\w*"))) {
        cout << "Found: " << string(m[0].first, m[0].second) << "\n";
    } else {
        cout << "Not found\n";
    }
    return 0;
}
KennyTM