# include "stdio.h"
# include "conio.h"
# include "string.h"
# define BUFFSIZE 100
main()
{
int m;
printf("please input the quantities of the students\n");
scanf("%d",&m);
char name[m][10],buff[BUFFSIZE];
int i,j,k,n;
for(i=0;i<m;i++)
{
printf("please input the name of the %dth students",i+1);
gets(buff);
if(strlen(name)>10)
{
printf("the name you input is larger than 10\n");
continue;
}
strcpy(name[i],buff);
}
for(j=0;j<m;j++)
{
for(i=0;i<m-j;i++)
{
if(name[i]>name[i+1])
{
strcpy(buff,name[i]);
strcpy(name[i],name[i+1]);
strcpy(name[i+1],buff);
}
这个那里有错误怎么改?
}
}
for(i=0;i<m;i++)
printf("%s\n",name[i]);
}
已经修改好了:
# include "stdio.h"
# include "conio.h"
# include "string.h"
# define BUFFSIZE 100
main()
{
int m,i,j,k,n;
char name[100][10],buff[BUFFSIZE];
printf("please input the quantities of the students\n");
scanf("%d",&m);
fflush(stdin); /*要清除缓冲区*/
for(i=0;i<m;i++)
{
printf("please input the name of the %dth students:",i+1);
gets(buff);
while(strlen(name)>10) /* 当长度>10时重新输入 */
{
printf("the name you input is larger than 10\n");
printf("please input the name of the %dth students:",i+1);
gets(buff);
}
strcpy(name[i],buff);
}
for(j=0;j<m-1;j++)
{
for(i=0;i<m-j;i++)
{
if(strcmp(name[i],name[i+1])>0) /* 比较字符串的大小 */
{
strcpy(buff,name[i]);
strcpy(name[i],name[i+1]);
strcpy(name[i+1],buff);
}
}
}
for(i=0;i<m;i++)
printf("%s\n",name[i]);
}