Advertisement
DaniDori

подсчет слов , упрощенный

Nov 15th, 2023
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4.  
  5. // Функция для удаления знаков препинания из слова
  6. std::string removePunctuation(const std::string& word) {
  7.     std::string result;
  8.     for (char c : word) {
  9.         if (isalnum(c) || c == '-') {
  10.             result += c;
  11.         }
  12.     }
  13.     return result;
  14. }
  15.  
  16. int main() {
  17.     std::string input;
  18.  
  19.     // Ввод строки
  20.     std::cout << "Введите строку: ";
  21.     std::getline(std::cin, input);
  22.  
  23.     // Подсчет количества слов
  24.     std::string word;
  25.     int wordCount = 0;
  26.  
  27.     for (size_t i = 0; i < input.size(); ++i) {
  28.         if (isalnum(input[i]) || input[i] == '-') {
  29.             word += input[i];
  30.         }
  31.         else if (!word.empty()) {
  32.             wordCount++;
  33.             word.clear();
  34.         }
  35.     }
  36.  
  37.     if (!word.empty()) {
  38.         wordCount++;
  39.     }
  40.  
  41.     std::cout << "Количество слов: " << wordCount << std::endl;
  42.  
  43.     // Подсчет количества уникальных слов
  44.     std::set<std::string> uniqueWords;
  45.     word.clear();
  46.  
  47.     for (size_t i = 0; i < input.size(); ++i) {
  48.         if (isalnum(input[i]) || input[i] == '-') {
  49.             word += input[i];
  50.         }
  51.         else if (!word.empty()) {
  52.             uniqueWords.insert(word);
  53.             word.clear();
  54.         }
  55.     }
  56.  
  57.     if (!word.empty()) {
  58.         uniqueWords.insert(word);
  59.     }
  60.  
  61.     std::cout << "Количество уникальных слов: " << uniqueWords.size() << std::endl;
  62.  
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement