2010-03-14 13 views

Répondre

3

Go sur votre chaîne à partir du premier caractère jusqu'au dernier. Vérifiez si vous rencontrez «w». Si oui, revenez en arrière jusqu'à ce que vous atteigniez un séparateur de mots (un espace par exemple) ou que vous atteigniez le début de votre chaîne, puis imprimez tous les caractères jusqu'à ce que vous rencontriez un autre séparateur de mots (ou la fin de la chaîne).

string Str; 
getline(cin, Str); 

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

Ou utilisez le find method de la classe de chaîne pour faire la recherche pour vous, alors il vous suffit d'identifier le mot.

1
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)); 
} 

Ces deux seulement des mots sur les espaces (I Missed que vous vouliez « xyzwy » au lieu de « xyzwy! » Au début), mais vous pouvez les modifier à ignorer la ponctuation. Le premier n'est pas très favorable à cela, mais le second pourrait être facilement modifié pour utiliser find_first/last_ pas _of avec l'équivalent de regex \w ("ABC..abc.012 .._") au lieu de vérifier les espaces. Notez que la seconde, utilisant la variable whitespace codée en dur, ne prend pas en compte les paramètres régionaux en tant que solution de flux (qui utilise les paramètres régionaux globaux définis en dernier), mais peut être exactement ce que vous voulez.

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

Si vous avez vraiment besoin regex, vous pouvez utiliser

\w*w\w* 

Par exemple:

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