From 9879b998b30d6cce30b747f67d93a6c485ff9b27 Mon Sep 17 00:00:00 2001 From: rhinemann Date: Mon, 16 Sep 2024 16:51:47 +0300 Subject: [PATCH] Finished strings task. --- strings.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/strings.c b/strings.c index b453418..7fd613a 100644 --- a/strings.c +++ b/strings.c @@ -1,6 +1,38 @@ #include +#include int main() { - printf("Hello, World!\n"); - return 0; + char str1[100], str2[100]; + + 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); + + + int max_str_len = (str1_len >= str2_len) ? str1_len : str2_len; + char res_str[max_str_len*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; + } + } + + printf("Added strings: %s\n", res_str); + + return 0; }