Facebook
From kaka, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 170
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. int countOccurrences(const std::string& text, const std::string& phrase) {
  6.     int count = 0;
  7.     size_t pos = text.find(phrase, 0);
  8.     while (pos != std::string::npos) {
  9.         count++;
  10.         pos = text.find(phrase, pos + 1);
  11.     }
  12.     return count;
  13. }
  14.  
  15. int main() {
  16.     std::string text = "Machina parowa przecież nie młynek do kawy, to wielka machina; ale gdy w niej zardzewieją kółka, stanie się gratem bezużytecznym i nawet niebezpiecznym. Otóż w Wokulskim jest podobne kółko, które rdzewieje i psuje się.";
  17.  
  18.     // Zadanie 1: Ilość liter 'a'
  19.     int countA = std::count(text.begin(), text.end(), 'a');
  20.     std::cout << "Ilość liter 'a' w tekście: " << countA << std::endl;
  21.  
  22.     // Zadanie 2: Ilość spacji
  23.     int countSpaces = std::count(text.begin(), text.end(), ' ');
  24.     std::cout << "Ilość spacji w tekście: " << countSpaces << std::endl;
  25.  
  26.     // Zadanie 3: Sprawdzenie występowania fraz
  27.     std::string phrases[] = {"nie", "tak", "stanie", "Wokulski"};
  28.     for (const std::string& phrase : phrases) {
  29.         int occurrences = countOccurrences(text, phrase);
  30.         std::cout << "Ilość wystąpień frazy '" << phrase << "': " << occurrences << std::endl;
  31.     }
  32.  
  33.     return 0;
  34. }