Embedded_Practices/strings.c

33 lines
877 B
C

#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Type the first string: \n");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0;
printf("Type the second string: \n");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0;
char* longer_string = (strlen(str1) >= strlen(str2)) ? str1 : str2;
char* shorter_string = (strlen(str1) >= strlen(str2)) ? str2 : str1;
char res_str[strlen(longer_string) * 2];
for (int i = 0; i < strlen(longer_string); i++) {
res_str[i*2] = str1[i];
res_str[i*2+1] = str2[i];
}
for (int i = 0; i < strlen(longer_string) - strlen(shorter_string) + 1; i++) {
res_str[2*strlen(shorter_string)+i] = longer_string[strlen(shorter_string)+i];
}
printf("Added strings: %s\n", res_str);
return 0;
}