Bugfixing

This commit is contained in:
rhinemann 2024-09-16 18:48:34 +03:00
parent 9879b998b3
commit ebb9a25e38
1 changed files with 10 additions and 16 deletions

View File

@ -7,29 +7,23 @@ int main() {
printf("Type the first string: \n");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0;
int str1_len = strlen(str1);
printf("Type the second string: \n");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0;
int str2_len = strlen(str2);
char* longer_string = (strlen(str1) >= strlen(str2)) ? str1 : str2;
char* shorter_string = (strlen(str1) >= strlen(str2)) ? str2 : str1;
int max_str_len = (str1_len >= str2_len) ? str1_len : str2_len;
char res_str[max_str_len*2];
char res_str[strlen(longer_string) * 2];
for (int i = 0; i < max_str_len; i++) {
if (i < str1_len && i < str2_len) {
res_str[i*2] = str1[i];
res_str[i*2+1] = str2[i];
} else if (i < str1_len && i > str2_len) {
res_str[i] = str1[i];
} else if (i > str1_len && i < str2_len) {
res_str[i] = str2[i];
} else if (i > str1_len && i > str2_len) {
res_str[i] = '\0';
break;
}
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);