C语言输入3个字符串,按由小到大的顺序输出
时间: 2023-09-09 12:08:02 浏览: 92
可以使用strcmp函数比较三个字符串的大小,并使用冒泡排序法将它们从小到大排列。
以下是示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[50], str2[50], str3[50], temp[50];
printf("请输入三个字符串:\n");
scanf("%s %s %s", str1, str2, str3);
// 冒泡排序法
if (strcmp(str1, str2) > 0) {
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
if (strcmp(str2, str3) > 0) {
strcpy(temp, str2);
strcpy(str2, str3);
strcpy(str3, temp);
}
if (strcmp(str1, str2) > 0) {
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
printf("排序后的结果为:\n%s\n%s\n%s", str1, str2, str3);
return 0;
}
```
输入三个字符串,程序会输出这三个字符串从小到大的顺序。