Guest User

Untitled

a guest
Apr 4th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int wordsFourLetters(const std::string& sentence)
  6. {
  7.     int words       = 0;
  8.     auto word_begin = sentence.cbegin();
  9.     auto word_end   = sentence.cbegin();
  10.     while (word_end != sentence.cend())
  11.     {
  12.         word_end = std::find_if(word_begin, sentence.cend(),
  13.                                 [](const auto& c) { return c == ' '; });
  14.        
  15.         if(std::distance(word_begin, word_end) == 4)
  16.             ++words;
  17.            
  18.         word_begin = word_end + 1;
  19.     }
  20.    
  21.     return words;
  22. }
  23.  
  24. int main()
  25. {
  26.     std::cout << wordsFourLetters("Hell World is word hell") << "=3";
  27. }
Advertisement
Add Comment
Please, Sign In to add comment