字符串排序(指向二维数组的指针
时间: 2023-12-14 15:34:13 浏览: 93
以下是使用指向二维数组的指针对字符串进行排序的示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 32
#define MAX_NUM 5
void sort(char (*str)[MAX_LEN], int n) {
int i, j;
char temp[MAX_LEN];
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
}
int main() {
char str[MAX_NUM][MAX_LEN];
int i, n;
printf("请输入字符串个数(不超过5个):");
scanf("%d", &n);
printf("请输入%d个字符串(每个字符串长度不超过32):\n", n);
for (i = 0; i < n; i++) {
scanf("%s", str[i]);
}
sort(str, n);
printf("排序后的字符串为:\n");
for (i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
return 0;
}
```