1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <iostream>
// replace punctuation with space, convert to lower case
std::string sanitize( const std::string& str )
{
std::string result ;
for( char c : str )
{
if( std::ispunct(c) ) result += ' ' ;
else result += std::tolower(c) ;
}
return result ;
}
// remove leading and trailing whitespace
std::string trim( std::string str )
{
std::stringstream stm ;
stm << str ;
stm >> str ;
return str ;
}
bool sentence_has_word( std::string sentence, std::string word )
{
sentence = sanitize(sentence) ;
word = trim( sanitize(word) ) ;
if( word.size() > sentence.size() ) return false ;
if( word.size() == sentence.size() ) return word == sentence ;
auto pos = sentence.find( word + ' ' ) ; // check for word at the beginning
if( pos == 0 ) return true ;
pos = sentence.find( ' ' + word + ' ' ) ; // check for word in the middle
if( pos != std::string::npos ) return true ;
word = ' ' + word ;
return sentence.substr( sentence.size() - word.size() ) == word ; // check for word at the end
}
int main()
{
const std::vector<std::string> sentences =
{
"The woods are lovely, dark, and deep, "
"But I have promises to keep, "
"And miles to go before I sleep.",
"You linger your little hour and are gone, "
"And still the woods sweep leafily on.",
"Two roads diverged in a wood, and I, "
"I took the one less traveled by, "
"And that has made all the difference."
};
const std::vector<std::string> words =
{ "wood", "Woods", "DARK", "be", "fore", " traveled ", " linger ", "two" };
for( std::string word : words )
{
std::cout << '"' << word << "\"\n" ;
for( std::string sentence : sentences )
if( sentence_has_word(sentence,word) ) std::cout << sentence << '\n' ;
std::cout << '\n' ;
}
}
|