代码如下
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void sort(char **p)
{
char **q, **s, *t;
for (q = p; q < p + 4; q++)
{
for (s = q + 1; s < p + 5; s++)
{
if (strcmp(*q, *s) > 0)
{
t = *q;
*q = *s;
*s = t;
}
}
}
}
int main()
{
char *a[5], b[5][99], **p;
int i;
for (i = 0; i < 5; i++)
a[i] = b[i];
printf("请依次输入五个字符串:\n");
for (i = 0; i < 5; i++)
scanf("%s", a[i]);
p = a;
sort(p);
printf("排序后输出为:\n");
for (i = 0; i < 5; i++)
{
printf("%s\n", a[i]);
}
system("pause");
return 0;
}
运行截图
思考反思
此题复习了指针相关的知识,对于运用指针解决问题也有了个很好的思路,后面应加强指针相关题的深究。