利用数组指针来对字符串排序

  1 #include<stdio.h>
  2 #include<string.h>
  3 #define N  3
  4 
  5 int main()
  6 {
  7     char str[N][20],*p[N],tmp[20];
  8     int i,j;
  9     for(i = 0;i < N;i++)
 10     {
 11     p[i] = str[i];
 12     scanf("%s",p[i]);
 13     }
 14     for(i = 0;i < N-1;i++)
 15     for(j = i + 1;j < N;j ++)
 16     {
 17     if(strcmp(p[i],p[j])>0)
 18     {
 19     strcpy(tmp,p[i]);
 20     strcpy(p[i],p[j]);
 21     strcpy(p[j],tmp);
 22     }
 23     }
 24     printf("输出排序后的字符;\n");
 25     for(i = 0;i < N;i++)
 26     printf("%s\n",p[i]);
 27     return 0;
 28 }
~                                                                                                              
~                

猜你喜欢

转载自blog.csdn.net/weixin_42720729/article/details/81227481