用c语言写程序,输入三个字符串,按从小到大的顺序输出
时间: 2024-03-30 22:11:18 浏览: 42
好的,这个问题很简单,下面是代码实现:
```
#include <stdio.h>
#include <string.h>
void swap(char *str1, char *str2) {
char temp[100];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
int main() {
char str1[100], str2[100], str3[100];
printf("请输入三个字符串:\n");
scanf("%s %s %s", str1, str2, str3);
if (strcmp(str1, str2) > 0) swap(str1, str2);
if (strcmp(str1, str3) > 0) swap(str1, str3);
if (strcmp(str2, str3) > 0) swap(str2, str3);
printf("三个字符串从小到大的顺序是:%s %s %s\n", str1, str2, str3);
return 0;
}
```
运行结果如下:
```
请输入三个字符串:
abc
def
ghi
三个字符串从小到大的顺序是:abc def ghi
```
相关问题
用c语言编写程序,输入三个字符串,按由小到大的顺序输出
好的,以下是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` 函数来交换两个字符串的值。
用c语言编写程序,输入三个字符串,按由小到大的顺序输出。
好的,以下是你需要的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char str1[MAX_LEN], str2[MAX_LEN], str3[MAX_LEN], temp[MAX_LEN];
scanf("%s %s %s", str1, str2, str3);
//排序
if (strcmp(str1, str2) > 0) {
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
if (strcmp(str1, str3) > 0) {
strcpy(temp, str1);
strcpy(str1, str3);
strcpy(str3, temp);
}
if (strcmp(str2, str3) > 0) {
strcpy(temp, str2);
strcpy(str2, str3);
strcpy(str3, temp);
}
//输出
printf("%s %s %s\n", str1, str2, str3);
return 0;
}
```
你可以将以上代码复制到C语言编译器中进行编译运行,输入三个字符串,程序会按照从小到大的顺序输出这三个字符串。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)