Advertisement
MeehoweCK

Untitled

Jul 26th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. string konwersja(string slowo)
  7. {
  8.     for(unsigned i = 0; i < slowo.size(); ++i)
  9.         if(slowo[i] >= 'A' && slowo[i] <= 'Z')
  10.             slowo[i] += 32;
  11.     string wynik = "";
  12.     for(unsigned i = 0; i < slowo.size(); ++i)
  13.         if((slowo[i] >= 'a' && slowo[i] <= 'z') || (slowo[i] >= '0' && slowo[i] <= '9'))
  14.             wynik += slowo[i];
  15.     return wynik;
  16. }
  17.  
  18. unsigned ilosc_slow(string wpis_slowo, ifstream& hamlet)
  19. {
  20.     unsigned i=0;
  21.     string slowa;
  22.     wpis_slowo = konwersja(wpis_slowo);
  23.     while(hamlet >> slowa)
  24.     {
  25.         if (wpis_slowo == konwersja(slowa))
  26.         {
  27.             i++;
  28.         }
  29.     }
  30.     hamlet.close();
  31.     return i;
  32. }
  33.  
  34. int main()
  35. {
  36.     string wpis_slowo;
  37.     cout << "Podaj szukane slowo: " << endl;
  38.     cin >> wpis_slowo;
  39.     ifstream plik;
  40.     plik.open("hamlet.txt");
  41.     if(plik.good())
  42.     {
  43.         cout << ilosc_slow(wpis_slowo, plik) << endl;
  44.         plik.close();
  45.     }
  46.  
  47.     /*string slowo = "SWORD.";
  48.     slowo = konwersja(slowo);
  49.     cout << slowo << endl;*/
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement