帮我写一个C语言程序:输入6个国家的名字,按字母顺序排列输出
时间: 2025-01-17 19:06:51 浏览: 49
当然可以,下面是一个简单的C语言程序,它会提示用户输入六个国家的名字,并按照字母顺序排序后显示出来:
```c
#include <stdio.h>
#include <string.h>
// 定义一个结构体存储国家名字
typedef struct {
char country[50];
} Country;
// 比较函数,用于qsort排序
int compare(const void* a, const void* b) {
Country c1 = *(Country*)a;
Country c2 = *(Country*)b;
return strcmp(c1.country, c2.country);
}
int main() {
// 初始化数组并设置元素个数
Country countries[6];
int i;
// 提示用户输入国家名并存储到结构体中
printf("请输入6个国家的名字(每个国家不超过49个字符):\n");
for (i = 0; i < 6; i++) {
scanf("%s", countries[i].country);
}
// 对数组进行排序
qsort(countries, 6, sizeof(Country), compare);
// 输出排序后的结果
printf("\n按字母顺序排列后的国家名:\n");
for (i = 0; i < 6; i++) {
printf("%s\n", countries[i].country);
}
return 0;
}
```
当你运行这个程序时,首先需要依次输入六个国家的名字,然后程序会自动按照字母顺序对它们进行排序并打印。
阅读全文
相关推荐

















