Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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";
- }
Advertisement
Add Comment
Please, Sign In to add comment