Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- # include <string.h>
- char* merge(char* str1, char* str2);
- int min(int a, int b){
- if (a<b)
- return a;
- else
- return b;
- }
- int main()
- {
- int str1_len;
- int str2_len;
- scanf("%i %i", &str1_len, &str2_len);
- char* str1;
- char* str2;
- // Выделить память и считать строки
- str1 =malloc(str1_len*sizeof(char));
- str2 =malloc(str2_len*sizeof(char));
- scanf("%s", str1);
- scanf("%s", str2);
- printf("%s", merge(str1, str2));
- }
- char* merge(char* str1, char* str2) {
- int i = 0;
- char *str = malloc((strlen(str2) + strlen(str1)+1) * sizeof(char));
- if (strlen(str2) == strlen(str1)) {
- for (int k = 0; k < (strlen(str2) + strlen(str1)); k += 2) {
- str[k] = str1[i];
- str[k + 1] = str2[i];
- i++;
- }
- str[strlen(str2) + strlen(str1)] = '\0';
- }
- i = 0;
- int m;
- if (strlen(str1) < strlen(str2)) {
- for (int k = 0; k < (strlen(str1)*2); k += 2) {
- str[k] = str1[i];
- str[k + 1] = str2[i];
- i++;
- m = k;
- }
- m+=2;
- for (int k = m;k < (strlen(str2) + strlen(str1)); k++){
- str[k] = str2[i];
- i++;
- }
- str[strlen(str2) + strlen(str1)] = '\0';
- }
- if (strlen(str1) > strlen(str2)) {
- for (int k = 0; k < (strlen(str2)*2); k += 2) {
- str[k] = str1[i];
- str[k+1] = str2[i];
- i++;
- m = k;
- }
- m+=2;
- for (int k = m;k < (strlen(str2) + strlen(str1)); k++){
- str[k] = str1[i];
- i++;
- }
- str[strlen(str2) + strlen(str1)] = '\0';
- }
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement