Embedded_Practices/strings.c

33 lines
877 B
C
Raw Permalink Normal View History

2024-09-16 14:46:12 +03:00
#include <stdio.h>
2024-09-16 16:51:47 +03:00
#include <string.h>
2024-09-16 14:46:12 +03:00
int main() {
2024-09-16 16:51:47 +03:00
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;
2024-09-16 18:48:34 +03:00
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];
2024-09-16 16:51:47 +03:00
}
printf("Added strings: %s\n", res_str);
return 0;
2024-09-16 14:46:12 +03:00
}