std::string get_word(const std::string& sentence, const std::string& substr)
{
std::string::size_type it = sentence.find(substr);
if(it == std::string::npos)
return "";
std::string::size_type it2 = it;
for(; it >= 1 && !std::isspace(sentence[it - 1]); --it);
for(; it2 <= sentence.size() - 1 && !std::isspace(sentence[it2 + 1]); ++it2);
return sentence.substr(it, (it2 + 1) - it);
}
int main()
{
std::string str("kchmviewer gnome-terminal");
std::cout << get_word(str, "viewer") << std::endl;
std::cout << get_word(str, "terminal") <<std::endl;
return 0;
}
Output will be:
kchmviewer
gnome-terminal