#include <stdio.h> #include <string.h> void fsort(char *color[ ], int n); int main(void ) { int i; char *pcolor[ ] = {"red", "blue", "yellow", "green", "black"}; void fsort(char *color[ ], int n); fsort(pcolor, 5); /* 调用函数 */ for(i = 0; i < 5; i++) printf("%s ", pcolor[i]); printf("\n"); return 0; } /* 请在这里填写答案 */
时间: 2024-01-13 08:03:17 浏览: 320
这段代码演示了如何使用函数对字符串数组进行排序。具体来说,函数`fsort`实现了对字符串指针数组的冒泡排序,并在`main`函数中调用该函数对字符串数组`pcolor`进行排序。最终,按照字典序输出排序后的字符串数组。
以下是完整的代码实现:
```c
#include <stdio.h>
#include <string.h>
void fsort(char *color[], int n);
int main(void)
{
int i;
char *pcolor[] = {"red", "blue", "yellow", "green", "black"};
fsort(pcolor, 5); /* 调用函数 */
for (i = 0; i < 5; i++)
printf("%s ", pcolor[i]);
printf("\n");
return 0;
}
void fsort(char *color[], int n)
{
int i, j;
char *temp;
for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
if (strcmp(color[i], color[j]) > 0)
{
temp = color[i];
color[i] = color[j];
color[j] = temp;
}
}
```
阅读全文