mardi 28 février 2017

Counting case-sensitive words in a string using C++

I want to count how many case-sensitive words are in string 's'. So far, I've transformed the punctuations into spaces and added each separate word in a set. Since, set only includes only unique elements, I am getting output 1.

How can I check the case-sensitivity of the words in string s?

int main() {
    string s = "ab\nAB!ab?AB:ab.AB;ab\nAB";

    transform(s.begin(), s.end(), s.begin(), [](char c)->char {
        if (isWordSeparator(c))
            return ' ';
    });

    istringstream iss(s);

    set<string> words((istream_iterator<string>(iss)), istream_iterator<string>());



    cout << "Number of Words: " << words.size() << endl;

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire