Advertisement
J3st3rs_j0k3

pr_teh_razv

Mar 9th, 2025
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. word_counter.h
  2. #ifndef WORD_COUNTER_H
  3. #define WORD_COUNTER_H
  4.  
  5. #include <string>
  6.  
  7. int countWords(const std::string& str);
  8.  
  9. #endif // WORD_COUNTER_H
  10.  
  11. word_counter.cpp
  12. #include "word_counter.h"
  13. #include <sstream>
  14.  
  15. int countWords(const std::string& str) {
  16.     std::istringstream stream(str);
  17.     std::string word;
  18.     int count = 0;
  19.  
  20.     while (stream >> word) {
  21.         count++;
  22.     }
  23.  
  24.     return count;
  25. }
  26.  
  27. main.cpp
  28. #include <iostream>
  29. #include "word_counter.h"
  30.  
  31. int main() {
  32.     std::string text;
  33.     std::cout << "Введите строку: ";
  34.     std::getline(std::cin, text);
  35.  
  36.     std::cout << "Количество слов: " << countWords(text) << std::endl;
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement