Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <locale.h>
- void merge(char *first, char *second, int second_len, int merge_pos, int curr);
- void find_merge_position(char *first, char *second, int first_len, int second_len, int curr);
- void parse_input(int curr);
- static char **result_strings;
- int main() {
- /// FUCK YOU
- system("chcp 1251 > 1.txt");
- int total;
- scanf_s("%i", &total);
- result_strings = (char **)malloc(sizeof(char *) * total);
- for (int i = 0; i < total; i++) {
- parse_input(i);
- }
- for (int i = 0; i < total; i++) {
- printf("%s\n", *(result_strings + i));
- }
- }
- void find_merge_position(char *first, char *second, int first_len, int second_len, int curr) {
- int res = first_len;
- for (int i = 0; i < first_len; i++) {
- // printf("%c, %c", first[i], second[0]);
- if (first[i] == second[0]) {
- int j = i;
- while (first[j] == second[j - i]) {
- j++;
- if (j - i >= second_len) {
- // we came over second string length
- break;
- }
- }
- if (j == first_len) {
- res = i;
- break;
- }
- }
- }
- merge(first, second, second_len, res, curr);
- }
- void merge(char *first, char *second, int second_len, int merge_pos, int curr) {
- char *result = (char *)malloc(sizeof(char) * (second_len + merge_pos + 1));
- for (int i = 0; i < merge_pos; i++) {
- result[i] = first[i];
- }
- for (int i = 0; i < second_len; i++) {
- result[i + merge_pos] = second[i];
- }
- result[merge_pos + second_len] = 0;
- result_strings[curr] = result;
- }
- void parse_input(int curr) {
- char *first = (char *)malloc(sizeof(char) * 1);
- char *second = (char *)malloc(sizeof(char) * 1);
- int first_length = 0, second_length = 0;
- char buffer = '1';
- while (buffer != ' ') {
- buffer = (char)getchar();
- if (buffer == '\n') {
- /**
- * after first scanf "enter" key there is '\n' symbol in input.
- * prevent reading it
- */
- continue;
- }
- // printf("%c, ", buffer);
- first = (char *)realloc(first, sizeof(char) * 1 + first_length);
- *(first + first_length++) = buffer != ' ' ? buffer : 0;
- }
- while (buffer != '\n') {
- buffer = (char)getchar();
- second = (char *)realloc(second, sizeof(char) * 1 + second_length);
- *(second + second_length++) = buffer != '\n' ? buffer : 0;
- }
- find_merge_position(first, second, first_length - 1, second_length - 1, curr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement