Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // laba8zad2.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "conio.h"
- #include "windows.h"
- #define BUFFER_SIZE 1024
- // дублирует слово word и заменяет по заданию
- void word_change(char *word, char *reword)
- {
- memset(reword, 0, 20); //memset заполняет первые 20 символов reword 0
- strncpy(reword, word + strlen(word) - 1, 1); //strncpy копирует 1 байт word + strlen(word) - 1 в reword
- strncat(reword, word, strlen(word) - 1); //strncat добавить к концу reword strlen(word) - 1 первых символов из word
- char *buffer = (char *)malloc(sizeof(char) * 20);
- for (int i = 0; reword[i] != '\0'; i++)
- {
- //возвращает адрес символа c в строке s, начиная с хвоста, или NULL, если строка s не содержит символ c
- if (((reword + i) != strrchr(reword, int(reword[i]))) && (strrchr(reword, reword[i]) != NULL)) // Если буква не единственная
- {
- memset(buffer, 0, 20);
- strncpy(buffer, reword, strrchr(reword, (reword[i])) - reword);
- strncat(buffer, strrchr(reword, (reword[i])) + 1, strlen(strrchr(reword, (reword[i]))) + 1);
- memset(reword, 0, 20);
- strncpy(reword, buffer, strlen(buffer));
- i = 0;
- }
- }
- }
- // Замена подстроки sub_str в строке str на sub_str_rep.
- bool find_and_insert(char* &str, char* sub_str, char* sub_str_rep)
- {
- char *buffer = NULL; // Буффер для
- char *search_substr = 0; // Найденная подстрока
- char Razdelitel[] = " !?/.,()[]{}"; // Символы разделители
- // если входные данные есть и строка найдена
- if (str && sub_str && sub_str_rep && strstr(str, sub_str))
- {
- // временный массив
- buffer = (char*)malloc(sizeof(char) * BUFFER_SIZE);
- if (!buffer)
- {
- printf("Не удалось выполнить операцию\n");
- return FALSE;
- }
- // чистим массив от мусора
- memset(buffer, 0, strlen(str));
- strncpy(buffer, str, strlen(str) - strlen(strstr(str, sub_str)));
- strcat(buffer, sub_str_rep);
- strcat(buffer, strstr(str, sub_str) + strlen(sub_str));
- strcpy(str, buffer);
- // освобождаем память отведённую под массив
- free(buffer);
- return TRUE;
- }
- return FALSE;
- }
- // Заменяет слова в строке str на преобразованные
- void change(char *&str)
- {
- char *str_copy = (char *)malloc(sizeof(char) * BUFFER_SIZE); // Создаем копию строки
- memset(str_copy, 0, BUFFER_SIZE); // Очистка от мусора
- strncpy(str_copy, str, strlen(str)); // Копирование str->str_copy
- char *reword = (char *)malloc(sizeof(char) * 20);
- char razdelitel[] = " !?/.,()[]{}";
- char *word = strtok(str, razdelitel);
- while (word != NULL)
- {
- word_change(word, reword);
- find_and_insert(str_copy, word, reword);
- word = strtok(NULL, razdelitel);
- }
- free(str);
- str = str_copy;
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- char *Str = (char*)malloc(sizeof(char)*BUFFER_SIZE);
- printf("Введите строку\n");
- gets_s(Str, BUFFER_SIZE);
- printf("\n");
- change(Str);
- puts(Str);
- free(Str);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement