1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
using namespace std;
 
class Histogram {
    string sentence;
public:
    Histogram(string sentence) { this->sentence = sentence; }
    void put(string s) { sentence += s; }
    void putc(char c) { sentence += c; }
    void print();
};
 
void Histogram::print() {
    int count = 0size = sentence.length();
    int alpha[26];
    fill_n(alpha, 260);    // 배열 전체를 0으로 초기화
    cout << sentence << endl << endl;
    for (int i = 0; i < size; i++) {
        if (isalpha(sentence[i])) {
            count++;
        }
    }
    cout << "총 알파벳 수 " << count << endl << endl;
    for (int i = 0; i < size; i++) {
        for (int j = 97; j <= 122; j++) {
            if (tolower(sentence[i]) == (char)j)
                alpha[j - 97]++;
        }
    }
    for (int i = 0; i < 26; i++) {
        cout << (char)(i + 97<< " (" << alpha[i] << ")\t: ";
        for (int j = 0; j < alpha[i]; j++) {
            cout << "*";
        }
        cout << endl;
    }
}
 
int main() {
    Histogram elvisHisto("Wise men say, only fools rush in But I can't help, ");
    elvisHisto.put("falling in love with you");
    elvisHisto.putc('-');
    elvisHisto.put("Elvis Presley");
    elvisHisto.print();
}
cs


+ Recent posts