c语言字符串排序问题!在线等!谢谢

来源:百度知道 编辑:UC知道 时间:2024/06/30 12:11:17
输入3个字符串,按降幂排列(比较每个字符串第一个字符,相同比较第2个,以此类推)

如: 输入 abc
acb
cba
输出 cba
acb
abc
不能用字符处理函数,帮写下程序与关键注释,谢谢!
strcmp函数不能用!!谢谢

给你

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define N 3
void sort_string(char *p[],int n); /*排序函数声明*/
int main()
{
char *p[N];/*定义指针数组*/
int i;
for(i=0;i<N;i++)/*动态分配空间*/
p[i]=(char*)malloc(21*sizeof(char));/*设字符串不超过20字节*/
printf("Input %d Strings:\n",N);
for(i=0;i<N;i++)
gets(p[i]);
sort_string(p,N);
printf("\nThe sequence after sort is:\n");
for(i=0;i<N;i++)
printf("%s\n",*(p+i));
system("pause");
return 0;
}
void sort_string(char *p[],int n)
{
char *t;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(*(p+k),*(p+j))>0) /* *(p+k)是p[k]指向的字符串的首地址*/
k=j;
if(k!=i)
{
t=*(p+i);
*(p+i)=*(p+k);
*(p+k)=t;
}
}