从键盘输入三组字符串并且由大到小排列输出
时间: 2023-08-05 08:22:48 浏览: 46
好的,以下是一个 Python 3 的示例程序,可以实现您的需求:
```python
strings = []
for i in range(3):
string = input("请输入第%d个字符串:" % (i+1))
strings.append(string)
strings.sort(reverse=True)
print("排序后的结果为:")
for string in strings:
print(string)
```
您可以将上述代码复制到 Python 3 环境中运行,也可以将其保存为 .py 文件后运行。程序会提示您输入三个字符串,然后按照从大到小的顺序排序并输出。
相关问题
用c语言写,从键盘输入3个字符串,按由小到大排序,使用strcmp函数
好的,以下是用C语言编写的程序,可以实现从键盘输入3个字符串,并按由小到大排序,使用strcmp函数:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[100], temp[100];
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(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("排序后的结果为:\n");
printf("%s\n%s\n%s\n", str1, str2, str3);
return 0;
}
```
在这个程序中,首先定义了4个字符数组,分别用于存储3个输入的字符串和一个中间变量。然后通过scanf函数从键盘输入3个字符串。接下来使用strcmp函数对3个字符串进行比较,并根据比较结果交换它们的位置,使得它们按由小到大的顺序排列。最后使用printf函数输出排序后的结果。
从键盘输入一个字符串按照字符顺序从小到大进行排列并删除重复的字符
可以使用以下代码实现:
```python
s = input() # 从键盘输入一个字符串
s = ''.join(sorted(set(s))) # 按照字符顺序从小到大进行排列并删除重复的字符
print(s)
```
这段代码首先使用 `input()` 函数从键盘读入一个字符串,然后使用 `set()` 函数将字符串中的重复字符去除,再使用 `sorted()` 函数将剩余的字符按照字符顺序从小到大进行排列,最后使用 `join()` 函数将排好序的字符拼接成一个新的字符串并输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)