【发布时间】:2023-04-03 05:20:01
【问题描述】:
按照学生姓氏的字母顺序对数组进行排序,并将数组打印到控制台。
但是代码没有正确显示输出,为什么?
如果我提供意见。假设,
输入:
Enter the value of number: 2
Please enter the name of the student:
Nihan ahmed
输出:
After sorting the array:
Nihan ahmed
为什么我不能输入多个名字?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void main()
{
char name[10][8], temp[8];
int i,j,n,L,k;
printf("Enter the value of number:\n");
scanf("%d", &n);
fflush(stdin);
printf("\n");
printf("Please enter the name of the student:\n");
for(i=0;i<n;i++)
{
gets(name[i]);
}
for(i=0;i<n-1;i++)
{
k=0;
while(1)
{
++k;
if(name[i][k]==' ')
break;
}
for(j=i+1;j<n;j++)
{
L=0;
while(1)
{
++L;
if(name[j][L]==' ')
break;
}
if(name[i][k+1]>name[j][L+1])
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("After sorting the array:\n");
for(i=0;i<n;i++)
{
puts(name[i]);
}
return 0;
}
【问题讨论】: