用c语言编写程序,输入三个字符串,按由小到大的顺序输出
时间: 2023-12-07 07:19:00 浏览: 128
好的,以下是C语言代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[100];
printf("请输入三个字符串,以空格隔开:");
scanf("%s %s %s", str1, str2, str3);
if (strcmp(str1, str2) > 0) {
char temp[100];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
if (strcmp(str2, str3) > 0) {
char temp[100];
strcpy(temp, str2);
strcpy(str2, str3);
strcpy(str3, temp);
}
if (strcmp(str1, str2) > 0) {
char temp[100];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
printf("从小到大的顺序为:%s %s %s", str1, str2, str3);
return 0;
}
```
这个程序会提示用户输入三个字符串,然后通过比较字符串大小,将它们按由小到大的顺序输出。需要注意的是,我们使用了 `strcmp` 函数来比较字符串大小,并使用了 `strcpy` 函数来交换两个字符串的值。
阅读全文
相关推荐















