Finished strings task.
This commit is contained in:
parent
04fd6c2065
commit
9879b998b3
34
strings.c
34
strings.c
|
@ -1,6 +1,38 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
printf("Hello, World!\n");
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue