今天我们完成一个在主函数10个等长的字符串。用另一个函数对他们排序。然后在主函数输出者10个已经排好的序的字符串
下面是我的代码
#include <stdio.h>
#include<string.h>
void sort(char s[10][6])
{
int i,j;
char *p;
char temp[10];
p=temp;
for (i = 0; i < 9; i++)
for (j = 0;j < 9-i; j++)
if (strcmp(s[j],s[j+1]) > 0)
{
strcpy(p,s[j]);
strcpy(s[j],s[+j+1]);
strcpy(s[j+1],p);
}
}
int main()
{
int i;
char str[10][6];
printf("input 10 strings:\n");
for (i = 0; i < 10; i++)
scanf("%s",str[i]);
sort(str);
printf("Now,the sequence is:\n");
for (i = 0;i < 10;i++)
printf("%s\n",str[i]);
return 0;
}
下面附上我的运行截图: