输入3个字符串,按照由小到大的顺序输出。C语言
时间: 2024-01-02 09:06:22 浏览: 25
以下是一个示例代码,可以实现输入3个字符串并按照字典序排序输出:
```
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100], str3[100];
printf("请输入三个字符串:\n");
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("按照字典序从小到大的顺序为:\n%s\n%s\n%s\n", str1, str2, str3);
return 0;
}
```
该代码使用了字符串库中的 `strcmp()` 函数来比较字符串的大小,并使用 `strcpy()` 函数来交换字符串的位置。