-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlettercount.cpp
More file actions
52 lines (45 loc) · 1.06 KB
/
lettercount.cpp
File metadata and controls
52 lines (45 loc) · 1.06 KB
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
47
48
49
50
51
52
#include <iostream>
#include <ctime>
using namespace std;
class LetterCounter {
char* letters;
int* counts;
public:
LetterCounter() {
letters = new char[100];
counts = new int[26]; // For 'a' to 'z'
for (int i = 0; i < 26; i++) {
counts[i] = 0;
}
}
void generateLetters() {
for (int i = 0; i < 100; i++) {
letters[i] = getRandomLowerCaseLetter();
}
}
char getRandomLowerCaseLetter() {
return 'a' + rand() % 26;
}
void countLetters() {
for (int i = 0; i < 100; i++) {
counts[letters[i] - 'a']++;
}
}
void displayCounts() {
for (int i = 0; i < 26; i++) {
cout << char('a' + i) << ": " << counts[i] << endl;
}
}
~LetterCounter() {
delete[] letters;
delete[] counts;
}
};
int main() {
srand(time(0)); // Seed for random number generator
LetterCounter counter;
counter.generateLetters();
counter.countLetters();
counter.displayCounts();
return 0;
}