#include <iostream>
#include <string>
#include <array>
constexpr int ASCII_RANGE = 256;
// Count characters and produce different outputs depending on mode
// Mode 0,1,2: fill freq array
// Mode 3,4: fill output string
void count_chars(const std::string& input, int mode, std::array<int, ASCII_RANGE>& freq, std::string& out_str) {
// Reset frequencies
freq.fill(0);
// Count frequencies
for (unsigned char ch : input) {
freq[ch]++;
}
if (mode == 3) { // used characters string
out_str.clear();
for (int i = 32; i < 127; i++) { // only printable
if (freq[i] > 0) {
out_str += static_cast<char>(i);
}
}
} else if (mode == 4) { // unused characters string
out_str.clear();
for (int i = 32; i < 127; i++) { // only printable
if (freq[i] == 0) {
out_str += static_cast<char>(i);
}
}
}
}
// Unified print function
// mode = 0 → all characters
// mode = 1 → only used characters
// mode = 2 → only unused characters
// printable_only = true → restrict to ASCII 32–126
void print_array(const std::array<int, ASCII_RANGE>& freq, int mode, bool printable_only) {
int start = printable_only ? 32 : 0;
int end = printable_only ? 127 : ASCII_RANGE;
for (int i = start; i < end; i++) {
bool condition = true;
if (mode == 1 && freq[i] <= 0) condition = false; // used only
if (mode == 2 && freq[i] != 0) condition = false; // unused only
if (condition) {
std::cout << "'" << static_cast<char>(i) << "' (" << i << "): " << freq[i] << '\n';
}
}
}
int main() {
const std::string text = "It's the perfect time to start";
std::array<int, ASCII_RANGE> freq;
std::string buffer;
// Mode 1: Only used characters and their frequencies
std::cout << "Mode 1: Used characters only\n";
count_chars(text, 1, freq, buffer);
print_array(freq, 1, true);
// Mode 2: Unused characters
std::cout << "\nMode 2: Unused characters\n";
count_chars(text, 2, freq, buffer);
print_array(freq, 2, true);
// Mode 3: String of used characters
std::cout << "\nMode 3: String of used characters: ";
count_chars(text, 3, freq, buffer);
std::cout << buffer << '\n';
// Mode 4: String of unused characters
std::cout << "\nMode 4: String of unused characters: ";
count_chars(text, 4, freq, buffer);
std::cout << buffer << '\n';
}
/*
run:
Mode 1: Used characters only
' ' (32): 5
''' (39): 1
'I' (73): 1
'a' (97): 1
'c' (99): 1
'e' (101): 4
'f' (102): 1
'h' (104): 1
'i' (105): 1
'm' (109): 1
'o' (111): 1
'p' (112): 1
'r' (114): 2
's' (115): 2
't' (116): 7
Mode 2: Unused characters
'!' (33): 0
'"' (34): 0
'#' (35): 0
'$' (36): 0
'%' (37): 0
'&' (38): 0
'(' (40): 0
')' (41): 0
'*' (42): 0
'+' (43): 0
',' (44): 0
'-' (45): 0
'.' (46): 0
'/' (47): 0
'0' (48): 0
'1' (49): 0
'2' (50): 0
'3' (51): 0
'4' (52): 0
'5' (53): 0
'6' (54): 0
'7' (55): 0
'8' (56): 0
'9' (57): 0
':' (58): 0
';' (59): 0
'<' (60): 0
'=' (61): 0
'>' (62): 0
'?' (63): 0
'@' (64): 0
'A' (65): 0
'B' (66): 0
'C' (67): 0
'D' (68): 0
'E' (69): 0
'F' (70): 0
'G' (71): 0
'H' (72): 0
'J' (74): 0
'K' (75): 0
'L' (76): 0
'M' (77): 0
'N' (78): 0
'O' (79): 0
'P' (80): 0
'Q' (81): 0
'R' (82): 0
'S' (83): 0
'T' (84): 0
'U' (85): 0
'V' (86): 0
'W' (87): 0
'X' (88): 0
'Y' (89): 0
'Z' (90): 0
'[' (91): 0
'\' (92): 0
']' (93): 0
'^' (94): 0
'_' (95): 0
'`' (96): 0
'b' (98): 0
'd' (100): 0
'g' (103): 0
'j' (106): 0
'k' (107): 0
'l' (108): 0
'n' (110): 0
'q' (113): 0
'u' (117): 0
'v' (118): 0
'w' (119): 0
'x' (120): 0
'y' (121): 0
'z' (122): 0
'{' (123): 0
'|' (124): 0
'}' (125): 0
'~' (126): 0
Mode 3: String of used characters: 'Iacefhimoprst
Mode 4: String of unused characters: !"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHJKLMNOPQRSTUVWXYZ[\]^_`bdgjklnquvwxyz{|}~
*/