Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <locale.h>
- #define MAX_LEN 100
- bool has_repeated_characters(const char *str, int len, char *repeated_char) {
- for (int i = 0; i < len; i++) {
- for (int j = i + 1; j < len; j++) {
- if (str[i] == str[j]) {
- *repeated_char = str[i];
- return true;
- }
- }
- }
- return false;
- }
- void process_word(const char *word) {
- int len = strlen(word);
- char protocol[MAX_LEN] = "";
- int start = 0, end = len - 1;
- bool forward = true;
- char temp[MAX_LEN];
- char repeated_char;
- while (start <= end) {
- int temp_len = 0;
- if (forward) {
- for (int i = start; i <= end; i++) {
- temp[temp_len++] = word[i];
- temp[temp_len] = '\0';
- if (has_repeated_characters(temp, temp_len, &repeated_char)) {
- protocol[strlen(protocol) - 1] = '\0';
- strcat(protocol, "_");
- start = i + 1;
- forward = false;
- break;
- }
- strncat(protocol, &word[i], 1);
- }
- if (forward) {
- break;
- }
- } else {
- for (int i = end; i >= start; i--) {
- temp[temp_len++] = word[i];
- temp[temp_len] = '\0';
- if (has_repeated_characters(temp, temp_len, &repeated_char)) {
- protocol[strlen(protocol) - 1] = '\0';
- strcat(protocol, "_");
- end = i - 1;
- forward = true;
- break;
- }
- strncat(protocol, &word[i], 1);
- }
- if (!forward) {
- break;
- }
- }
- }
- printf("«Протокол» просмотра: %s\n", protocol);
- }
- int main() {
- system("chcp 1251");
- setlocale(LC_ALL, "");
- char word[MAX_LEN];
- printf("Введите слово (только прописные русские буквы): ");
- scanf("%s", word);
- for (int i = 0; word[i] != '\0'; i++) {
- if (word[i] < 'А' || word[i] > 'Я') {
- printf("Ошибка: допустимы только прописные русские буквы.\n");
- return 1;
- }
- }
- process_word(word);
- int n;
- scanf("%d", &n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement