Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- word_counter.h
- #ifndef WORD_COUNTER_H
- #define WORD_COUNTER_H
- #include <string>
- int countWords(const std::string& str);
- #endif // WORD_COUNTER_H
- word_counter.cpp
- #include "word_counter.h"
- #include <sstream>
- int countWords(const std::string& str) {
- std::istringstream stream(str);
- std::string word;
- int count = 0;
- while (stream >> word) {
- count++;
- }
- return count;
- }
- main.cpp
- #include <iostream>
- #include "word_counter.h"
- int main() {
- std::string text;
- std::cout << "Введите строку: ";
- std::getline(std::cin, text);
- std::cout << "Количество слов: " << countWords(text) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement