Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const char EOT = '\0';
  6.  
  7. struct appear {
  8.     char ch;
  9.     int n_appearances;
  10. };
  11. typedef struct appear APPEAR;
  12.  
  13. void appearances(char* str, APPEAR appear_arr[], int &n);
  14.  
  15. void appearances(char* str, APPEAR appear_arr[], int &n) {
  16.     if (*str == EOT) {
  17.         n = 0;
  18.     } else {
  19.         appearances(str + 1, appear_arr, n);
  20.         for (int i = 0; i < n; i++) {
  21.             if (appear_arr[i].ch == *str) {
  22.                 appear_arr[i].n_appearances++;
  23.                 return;
  24.             }
  25.         }
  26.         appear_arr[n].ch = *str;
  27.         appear_arr[n].n_appearances = 1;
  28.         n++;
  29.     }
  30. }
  31.  
  32. // prints out the amount of times each letter appears in the input string
  33. // input: a string
  34. // output: the amount of times each letter appears in the input string
  35. int main()
  36. {
  37.     const int SIZE = 100;
  38.     char str[SIZE];
  39.     int n;
  40.     APPEAR res[SIZE];
  41.     cout << "enter a string: ";
  42.     cin.getline(str, SIZE);
  43.  
  44.     appearances(str, res, n);
  45.  
  46.     for (int i = 0; i < n; i++) {
  47.         cout << res[i].ch << ":" << res[i].n_appearances << " ";
  48.     }
  49.     cout << endl;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement