C语言2、 已知字符串1为:"fine!", 字符串2为:"How are you?",试将这两个字符串连接成一个字符串,要求长的字符串放到前面。试编程输出这个字符串和这三个字符串的长度。
时间: 2023-06-25 17:03:21 浏览: 79
下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "fine!";
char str2[] = "How are you?";
char result[100];
if (strlen(str1) >= strlen(str2)) {
strcpy(result, str1);
strcat(result, str2);
} else {
strcpy(result, str2);
strcat(result, str1);
}
printf("Result: %s\n", result);
printf("Length of result: %d\n", strlen(result));
printf("Length of str1: %d\n", strlen(str1));
printf("Length of str2: %d\n", strlen(str2));
return 0;
}
```
输出结果为:
```
Result: How are you?fine!
Length of result: 17
Length of str1: 5
Length of str2: 12
```
阅读全文