Advertisement
AlexandruFilipescu

C++ count letters and order them from an input file

Aug 3rd, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. struct litera{
  6.     int ct;
  7.     int numarul;
  8. };
  9.  
  10. int main() {
  11.     litera l[10];
  12.     ifstream f("engleza.txt");
  13.     ofstream o("engleza-iesire.txt");
  14.     char S[100];
  15.     int ct[256], i,j,k,temp;
  16.  
  17.  
  18.     for (i = 0; i < 256;i++) {
  19.         ct[i] = 0;
  20.     }
  21.  
  22.     while (!f.eof()) {
  23.         f.getline(S, 100);
  24.         for (i = 0; S[i];i++) {
  25.             if( (S[i] >= 'a') && (S[i] <='z')){
  26.             ct[S[i]]++;
  27.             }
  28.         }
  29.     }
  30.  
  31.     j = 0;
  32.  
  33.     for (i = 0; i < 256;i++) {
  34.         if (ct[i]) {
  35.             l[j].numarul = i;
  36.             l[j].ct = ct[i];
  37.             j++;
  38.         }
  39.     }
  40.  
  41.     cout << endl;
  42.  
  43.     for (k = 0; k < j - 1;k++) {
  44.         for (i = 0; i < j - 1; i++) {
  45.             if (l[i].ct < l[i+1].ct) {
  46.                 temp = l[i].ct;
  47.                 l[i].ct = l[i + 1].ct;
  48.                 l[i + 1].ct = temp;
  49.                 temp = l[i].numarul;
  50.                 l[i].numarul = l[i + 1].numarul;
  51.                 l[i + 1].numarul = temp;
  52.             }
  53.         }
  54.     }
  55.  
  56.     for (i = 0; i < j;i++) {
  57.         cout << char(l[i].numarul) << " " << l[i].ct;
  58.         cout << endl;
  59.       }
  60.  
  61.  
  62.     system("pause");
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement