#include<stdio.h>//选的第四题 #include<string.h> #define N 100 void cmp_paixu(char a[N][15]) { int i,j; char *b; for(i=0;i<N-1;i++) { for(j=0;j<N-1-i;j++) if(strcmp(a[j],a[j+1])<0) { strcpy(b,a[j]); strcpy(a[j],a[j+1]); strcpy(a[j+1],b); } } } void output(char a[N][15]) { int i; for(i=0;i<N;i++) printf("%s\n",a[i]); } int main() { char name[N][15]={'Beijing','Shanghai','Nanjing','Changzhou','Suzhou','Wuxi','Shenzhen','Guangzhou','Qingdao','Xiamen','Chongqing'}; printf("排序后为:\n"); cmp_paixu(name); output(name); return 0; }
时间: 2024-04-27 17:23:10 浏览: 20
这是一个使用字符串数组进行排序的程序,其中定义了一个常量 N 为 100,表示字符串数组的大小。程序中定义了两个函数,cmp_paixu 和 output,分别用于排序和输出,主函数中定义了一个字符串数组 name,其中存储了 11 个城市的名称。程序通过调用 cmp_paixu 函数将字符串数组按字典序进行降序排序,然后通过调用 output 函数将排序后的结果输出。最后程序返回 0,表示程序执行成功。
相关问题
#include<stdio.h>#include<stdlib.h>#include<string.h>
在C语言中,`#include<stdio.h>`、`#include<stdlib.h>`和`#include<string.h>`分别包含了输入/输出、内存管理以及字符串处理相关的库。这里给你演示如何使用这些头文件中的函数:
1. **输入/输出(stdio.h)**:
```c
#include<stdio.h>
int main() {
printf("Hello, World!\n"); // 使用printf输出字符串
scanf("%s", "Enter your name: "); // 使用scanf从用户那里读取输入
return 0;
}
```
2. **内存管理(stdlib.h)**:
```c
#include<stdlib.h>
int *createArray(int size) {
int *array = malloc(size * sizeof(int)); // 分配动态内存
return array;
}
void freeMemory(int *array, int size) {
free(array); // 释放已分配的内存
}
```
3. **字符串处理(string.h)**:
```c
#include<string.h>
char* copyString(const char* src) {
char* dest = (char*)malloc(strlen(src) + 1); // 动态分配内存复制字符串
strcpy(dest, src);
return dest;
}
```
注意,C语言的标准库函数可能需要链接到适当的库,如`-lstdc++` 或 `-lm`,具体取决于编译器设置。
#include<stdio.h> #include<malloc.h> #include<string.h> #define MAX_SIZE 50
`#include<stdio.h>` 这行头文件包含了标准输入输出库,用于程序中的基本输入输出操作,比如 printf 和 scanf 函数。
`#include<malloc.h>` 包含了内存管理函数,如 malloc 和 free,这些函数允许程序动态地分配和释放内存,以便在运行时根据需要增加或减少内存使用。
`#include<string.h>` 包含了字符串处理函数的集合,如 strlen、strcpy、strcat 等,它们用于处理以字符数组形式表示的字符串。
`#define MAX_SIZE 50` 这是一个预处理器宏定义,用来设置一个最大值(这里是50),通常在需要限制某个数组或字符串长度的地方使用。例如,如果有一个需要存放字符串的数组,可能会用这个宏来指定其最大容量。
相关问题--
1. 为什么在程序中经常看到 #define 宏定义?
2. 在什么情况下会用到 `malloc()` 函数?
3.
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)