用c语言编写 从键盘输入n个字符不超过80个,将他们从大到小进行排序
时间: 2024-05-08 13:14:50 浏览: 34
```c
#include <stdio.h>
#include <string.h>
#define MAX_N 80
void sort(char str[], int n);
int main() {
char str[MAX_N + 1];
int n;
printf("请输入 n:");
scanf("%d", &n);
printf("请输入 %d 个字符:", n);
scanf("%s", str);
sort(str, n);
printf("排序后的字符串为:%s\n", str);
return 0;
}
void sort(char str[], int n) {
int i, j;
char temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (str[i] < str[j]) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
```
相关问题
用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函数输出排序后的结果。
用C语言编写程序:从键盘输入4个字符串(长度<20),存入二维字符数组中。然后对他们由小到大进行排序,最后输出排序后的四个字符串
好的,这是一个简单的字符串排序程序,你可以参考以下代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 20
#define NUM_STRINGS 4
int main()
{
char strings[NUM_STRINGS][MAX_LEN];
char temp[MAX_LEN];
// 输入字符串
for (int i = 0; i < NUM_STRINGS; i++) {
printf("请输入第 %d 个字符串:", i + 1);
scanf("%s", strings[i]);
}
// 排序字符串
for (int i = 0; i < NUM_STRINGS - 1; i++) {
for (int j = i + 1; j < NUM_STRINGS; j++) {
if (strcmp(strings[i], strings[j]) > 0) {
strcpy(temp, strings[i]);
strcpy(strings[i], strings[j]);
strcpy(strings[j], temp);
}
}
}
// 输出排序后的字符串
printf("排序后的字符串:\n");
for (int i = 0; i < NUM_STRINGS; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
```
代码中,我们首先定义了一个二维字符数组 `strings` 来存储输入的字符串,以及一个临时字符数组 `temp` 用于交换字符串。然后通过循环输入四个字符串,接着使用双重循环对四个字符串进行排序,最后输出排序后的四个字符串。
注意,代码中我们使用了 `strcmp` 函数来比较两个字符串的大小关系,`strcpy` 函数来拷贝字符串。同时,我们限制了字符串长度不超过 `MAX_LEN`,输入的字符串个数为 `NUM_STRINGS`。
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.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)