Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const char EOT = '\0';
- struct appear {
- char ch;
- int n_appearances;
- };
- typedef struct appear APPEAR;
- void appearances(char* str, APPEAR appear_arr[], int &n);
- void appearances(char* str, APPEAR appear_arr[], int &n) {
- if (*str == EOT) {
- n = 0;
- } else {
- appearances(str + 1, appear_arr, n);
- for (int i = 0; i < n; i++) {
- if (appear_arr[i].ch == *str) {
- appear_arr[i].n_appearances++;
- return;
- }
- }
- appear_arr[n].ch = *str;
- appear_arr[n].n_appearances = 1;
- n++;
- }
- }
- // prints out the amount of times each letter appears in the input string
- // input: a string
- // output: the amount of times each letter appears in the input string
- int main()
- {
- const int SIZE = 100;
- char str[SIZE];
- int n;
- APPEAR res[SIZE];
- cout << "enter a string: ";
- cin.getline(str, SIZE);
- appearances(str, res, n);
- for (int i = 0; i < n; i++) {
- cout << res[i].ch << ":" << res[i].n_appearances << " ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement