lol
unknown
c_cpp
2 years ago
6.2 kB
5
Indexable
#include <iostream> void input_text(char** str, int& lenght); void input_int(int& a); void input_str(char* str); bool check_end(char* str); void find_and_replace_words_in_string(char* str); void replace_word(char* str, char* substr, int left, int right, int n); void write_text(char** text, int lenght); void delete_text(char** text); int strlen(char* str); void turn_all_chars_left(char* str, int left, int turn); void turn_all_chars_right(char* str, int left, int turn); void check_exit(bool& exit); int main() { std::cout << "The task was completed by a student of the 353505 group - Denis Anufriev\n\n" << "The program processes an array of strings (the length of each string is no more than 100 characters).\n" << "If a string contains a sequence of identical characters, the program replaces them with code 255,\n" << "followed by the code of this character and the number of identical characters..\n\n"; bool exit; check_exit(exit); while (!exit) { int lenght; char* str = new char[1001]; char** text = new char* [100]; for (int i = 0; i < 100; i++) text[i] = new char[1000]; std::cout << "Enter your text (if you have finished typing, type 'end.' in the last separate line)\n"; input_text(text, lenght); for (int i = 0; i < lenght; i++) { find_and_replace_words_in_string(text[i]); } write_text(text, lenght); delete_text(text); check_exit(exit); } return 0; } void input_text(char** str, int& lenght) { bool isTyping = true; for (int i = 0; isTyping; i++) { bool isCorrect; int index = 0; char c = getchar(); do { isCorrect = true; str[i][index] = c; index++; if (index > 100) { isCorrect = false; index = 0; while ((c = getchar()) && c != '\n'); std::cout << "The number of characters in a line of text exceeds 100 characters!\n"; } } while (((c = getchar()) && c != '\n') || !isCorrect); str[i][index] = '\0'; if (check_end(str[i])) { isTyping = false; lenght = i; } } } void input_int(int& a) { char* str = new char[1001](); bool isIncorrect; do { isIncorrect = false; input_str(str); for (int i = 0; str[i] != '\0'; i++) { if ((str[i] > '9' || str[i] < '0') && str[i] != '+') { std::cout << "You entered a non-natural number! Repeat the input: "; isIncorrect = true; break; } } a = atoi(str); } while (isIncorrect); } void input_str(char* str) { bool isCorrect; int index = 0; char c = getchar(); do { isCorrect = true; str[index] = c; index++; if (index > 1000) { isCorrect = false; index = 0; while ((c = getchar()) && c != '\n'); std::cout << "The number of characters in a string exceeds 1000 characters!\n"; } } while (((c = getchar()) && c != '\n') || !isCorrect); str[index] = '\0'; } void find_and_replace_words_in_string(char* str) { bool isOkay = true, searching_left = true, searching_right = true; int left = 0, right = 0; char symbol; char* substr = new char[300](); for (int i = 0; isOkay; i++) { if (str[i] != '\0') { if (searching_left && str[i + 1] == str[i]) { symbol = str[i]; left = i; searching_left = false; } else if (!searching_left && searching_right) { if (str[i] == symbol) { right = i; } else searching_right = false; } } else if ((str[i] == '\0') && !searching_left && searching_right) { searching_right = false; if (str[i] == '\0') isOkay = false; } if (!searching_left && !searching_right) { int value = right - left + 1; if (value > 1) { substr[0] = char(255); substr[1] = symbol; if (value < 10) { substr[2] = (char(value) + '0'); substr[3] = '\0'; } else if (value >= 10 && value < 100) { substr[2] = (char(value / 10) + '0'); substr[3] = (char(value % 10) + '0'); substr[4] = '\0'; } else { substr[2] = '1'; substr[3] = '0'; substr[4] = '0'; substr[5] = '\0'; } replace_word(str, substr, left, right, value); if (strlen(substr) < value) { i -= (value - strlen(substr)); } else if (strlen(substr) > value) { i += (strlen(substr) - value); } } searching_left = true; searching_right = true; } } } int strlen(char* str) { bool search = true; int lenght; for (int i = 0; search; i++) { if (str[i] == '\0') { search = false; lenght = i; } } return lenght; } void turn_all_chars_left(char* str, int left, int turn) { bool isOkay = true; for (int i = left; isOkay; i++) { str[i] = str[i + turn]; if (str[i] == '\0') { str[i + turn] = 'a'; isOkay = false; } } } void turn_all_chars_right(char* str, int left, int turn) { bool isOkay = true; for (int i = strlen(str) + turn; isOkay; i--) { str[i] = str[i - turn]; if (i - turn == left) isOkay = false; } } void replace_word(char* str, char* substr, int left, int right, int n) { int lensubstr = strlen(substr); if (lensubstr == n) { for (int i = left; i <= right; i++) { str[i] = substr[i - left]; } } else if (lensubstr < n) { for (int i = left; i <= left + (lensubstr - 1); i++) { str[i] = substr[i - left]; } turn_all_chars_left(str, left + lensubstr, n - lensubstr); } else if (lensubstr > n) { turn_all_chars_right(str, right + 1, lensubstr - n); for (int i = left; i <= left + (lensubstr - 1); i++) { str[i] = substr[i - left]; } } } void write_text(char** text, int lenght) { std::cout << "The received text:\n"; for (int i = 0; i < lenght; i++) { for (int j = 0; text[i][j] != '\0'; j++) { std::cout << text[i][j]; } std::cout << '\n'; } } void delete_text(char** text) { for (int i = 0; i < 100; i++) { delete[] text[i]; } delete[] text; } bool check_end(char* str) { if (strncmp(str, "end.", 4) == 0) return true; else return false; } void check_exit(bool& exit) { int check_exit = -1; std::cout << "Enter 1 to start executing the program function.\nEnter 0 to exit the program\n\n"; while (check_exit != 0 && check_exit != 1) { input_int(check_exit); if (check_exit != 0 && check_exit != 1) std::cout << "You entered neither 1 nor 0!\n"; } exit = (check_exit) ? false : true; }
Editor is loading...
Leave a Comment