C语言输入3个字符串,按由小到大顺序输出。
时间: 2023-10-31 18:14:07 浏览: 53
可以使用标准库中的`strcmp`函数来进行字符串的比较,然后根据比较结果来进行排序。下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char str1[MAX_LEN], str2[MAX_LEN], str3[MAX_LEN];
printf("Please input three strings:\n");
scanf("%s%s%s", str1, str2, str3);
if (strcmp(str1, str2) > 0) {
char temp[MAX_LEN];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
if (strcmp(str1, str3) > 0) {
char temp[MAX_LEN];
strcpy(temp, str1);
strcpy(str1, str3);
strcpy(str3, temp);
}
if (strcmp(str2, str3) > 0) {
char temp[MAX_LEN];
strcpy(temp, str2);
strcpy(str2, str3);
strcpy(str3, temp);
}
printf("The sorted strings are:\n");
printf("%s\n%s\n%s\n", str1, str2, str3);
return 0;
}
```
示例输出:
```
Please input three strings:
hello
world
apple
The sorted strings are:
apple
hello
world
```