定义三个字符串,st1,str2,str3,分别为三个字符串赋值,通过strcmp和strcpy函数对字符串进行由小到大排序。
#include <stdio.h>
#include <string.h>
void sort(char c[][81], int n)
{
int i,j,k;
char t[81];
for(i=0; i<n-1; i++)
{
k=i;
for(j=i+1; j<=n; j++)
{
if(strcmp(c[k],c[j]) > 0)
{
k=j;
}
}
if(k!=i )
{
strcpy(t,c[i]);
strcpy(c[i],c[k]);
strcpy(c[k],t);
}
}
}
int main()
{
int i;
char c[3][81]={0};
for(i=0; i<3; i++)
scanf("%s",&c[i]);
sort(c,3) ;
printf("After sorted:\n");
for(i=0; i<3; i++)
printf("%s\n",c[i]);
return 0;
}