minerva
my_string.cpp
4.1 KB · 180 lines
1#include <iostream>23const int MAX_SIZE = 256;45class My_String {6private:7 char input_buffer[MAX_SIZE];8 char original[MAX_SIZE];910public:11 // constructor: copy the input char by char into input_buffer 12 My_String(const char input[]) {13 int i = 0;1415 while (i < MAX_SIZE - 1 && input[i] != '\0') {16 input_buffer[i] = input[i];17 original[i] = input[i];18 i++;19 }20 input_buffer[i] = '\0';21 original[i] = '\0';22 }2324 // method 1: count characters until '\0'25 int length() {26 int count = 0;2728 for (;;) {29 if (input_buffer[count] == '\0') {30 break;31 }32 count++;33 }34 return count;35 }3637 // method 2: check if the string is all letters38 bool is_alpha() {39 bool all_letters = true;4041 for (int i = 0; input_buffer[i] != '\0'; i++) {42 char c = input_buffer[i];4344 // A-Z is 65-90, 45 // a-z is 97-12246 if (!((c >= 65 && c <= 90) || (c >= 97 && c <= 122))) {47 std::cout << "not a letter at index " << i << ": '" << c << "'\n";48 all_letters = false;49 }50 }5152 if (all_letters) {53 std::cout << "string is all letters\n";54 } else {55 std::cout << "string is not all letters\n";56 }57 return all_letters;58 }5960 // method 3: convert a-z to A-Z by subtracting 3261 void to_upper() {62 for (int i = 0; input_buffer[i] != '\0'; i++) {63 if (input_buffer[i] >= 97 && input_buffer[i] <= 122) {64 input_buffer[i] = input_buffer[i] - 32;65 }66 }67 std::cout << input_buffer << "\n";68 }6970 // method 4: convert A-Z to a-z by adding 3271 void to_lower() {72 for (int i = 0; input_buffer[i] != '\0'; i++) {73 if (input_buffer[i] >= 65 && input_buffer[i] <= 90) {74 input_buffer[i] = input_buffer[i] + 32;75 }76 }77 std::cout << input_buffer << "\n";78 }7980 // method 5: put c where '\0' was, then add a new '\0' after it81 void push_back(char c) {82 int end = length();8384 // last slot is saved for '\0'85 if (end >= MAX_SIZE - 1) {86 std::cout << "buffer is full, can't add " << c << "\n";87 return;88 }8990 input_buffer[end] = c;91 input_buffer[end + 1] = '\0';92 std::cout << input_buffer << "\n";93 }9495 // method 6: output original string96 void output_original() { std::cout << "original: " << original << "\n"; }9798 // method 7: output current string99 void output_current() { std::cout << "current: " << input_buffer << "\n"; }100};101102int main() {103 char input[MAX_SIZE];104105 std::cout << "enter a string: ";106 std::cin.getline(input, MAX_SIZE);107108 // input was longer than the buffer: keep what fit, drop the rest109 if (std::cin.fail()) {110 std::cin.clear();111 std::cin.ignore(10000, '\n');112 }113114 My_String my_string(input);115 my_string.output_original();116117 int choice = -1;118119 while (choice != 0) {120 std::cout << "\n----- menu -----\n";121 std::cout << "1. length\n";122 std::cout << "2. is alpha\n";123 std::cout << "3. to upper\n";124 std::cout << "4. to lower\n";125 std::cout << "5. push back\n";126 std::cout << "6. show original and current\n";127 std::cout << "0. exit\n";128 std::cout << "choice: ";129130 // not a number: clear the error and throw away the bad input131 if (!(std::cin >> choice)) {132 if (std::cin.eof()) {133 break;134 }135 std::cin.clear();136 std::cin.ignore(10000, '\n');137 std::cout << "invalid choice, enter a number\n";138 choice = -1;139 continue;140 }141142 // remove the leftover newline143 std::cin.ignore(10000, '\n');144145 switch (choice) {146 case 1:147 std::cout << "length: " << my_string.length() << "\n";148 break;149 case 2:150 my_string.is_alpha();151 break;152 case 3:153 my_string.to_upper();154 break;155 case 4:156 my_string.to_lower();157 break;158 case 5: {159 char c;160 std::cout << "enter one character: ";161 std::cin >> c;162 std::cin.ignore(10000, '\n');163 my_string.push_back(c);164 break;165 }166 case 6:167 my_string.output_original();168 my_string.output_current();169 break;170 case 0:171 break;172 default:173 std::cout << "invalid choice\n";174 }175 }176177 return 0;178}179180// g++ -std=c++17 -Wall -o my_string my_string.cpp