Counting Four-Letter Words in C++
#include <algorithm> #include <iostream> #include <string> int wordsFourLetters(const std::string& sentence) { int words = 0; auto word_begin = sentence.cbegin(); auto word_end = sentence.cbegin(); while (word_end != sentence.cend()) { word_end = std::find_if(word_begin, sentence.cend(), [](const auto& c) { return c == ' '; }); if(std::distance(word_begin, word_end) == 4) ++words; word_begin = word_end + 1; } return words; } int main() { std::cout << wordsFourLetters("Hell world is word hell") << "=3";
A C++ code screenshot defines a function that counts four-letter words in a sentence. The example string contains “Hell,” “word,” and “hell,” producing the punchline “=3,” humorously treating the repeated misspelled or provocative words as four-letter words. EN: A C++ code screenshot defines a function that counts four-letter words in a sentence. The example string contains “Hell,” “word,” and “hell,” producing the punchline “=3,” humorously treating the repeated misspelled or provocative words as four-letter words.
- @ithueti / 775 2019-04-04
- Type
- image
- Size
- 541×648 · 36.8 KiB
- Mood
- absurd, sarcastic
- Language
- en
- Added