‘壹’ c语言字符串排序
你有两个错误,一是C的变量定义必须在所有语句之前,二是字符串不能赋值,必须使用strcpy函数,修改后的程序代码和运行结果如下:
$ cat a.c
#include<stdio.h>
#include<string.h>
void Solve();
void sort(char *name[], int n);
int main()
{
Solve();
return 0;
}
void Solve()
{
int i,n;
char name[100][100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
gets(name[i]);
}
sort(name,n);
for(i=0;i<n;i++)
{
puts(name[i]);
}
}
void sort(char *name[100], int n)
{
int i,j,k;
char *temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++) if(strcmp(name[k],name[j])>0) k=j;
if (k!=i)
{
{
strcpy(temp,name[i]);
strcpy(name[i],name[k]);
strcpy(name[k],temp);
}
}
}
}
$ cc a.c
"a.c", line 19: warning #2167-D: argument of type "char (*)[100]" is
incompatible with parameter of type "char **"
sort(name,n);
^
$ a.out
6dkjlfhgjkshdfjkgh
1kdfghj
3dkjfhghd
2dlkfjgklj
5ldfkhlkj
4dflkghklj
1kdfghj
3dkjfhghd
2dlkfjgklj
5ldfkhlkj
4dflkghklj
$ cc -V
cc: HP C/aC++ B3910B A.06.25 [Nov 30 2009]
/cq/u/cqjsdba/ygb>
‘贰’ c语言字符串排序
#include<stdio.h>
#include<string.h>
#define SIZE 91
#define LIM 31
#define HALT""
void stsrt(char*strings[],int num);
int main(void)
{
char input[LIM][SIZE];
char*ptstr[LIM];
int ct=0;
int k=0;
printf("input up to%d lines,and I will sort them. ",LIM);
printf("To stop,press the enter key at a line's start. ");
while(ct<LIM&&gets_s(input[ct],100)!=NULL&&input[ct][0]!='