當前位置:成語大全網 - 新華字典 - C語言,數組。用鍵盤輸入n個學生的姓名,並按字典順序排序輸出,n值由鍵盤輸入。

C語言,數組。用鍵盤輸入n個學生的姓名,並按字典順序排序輸出,n值由鍵盤輸入。

#include <stdio.h>

#include<string.h>

int main ()

{int n,i,j;

char s[100][20],t[20];

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%s",s[i]);

for(i=0;i<n-1;i++)

for(j=0;j<n-1-i;j++)

if(strcmp(s[j],s[j+1])>0)

{strcpy(t,s[j]);

strcpy(s[j],s[j+1]);

strcpy(s[j+1],t);

}

printf("\n\nAfter:\n");

for(i=0;i<n;i++)

puts(s[i]);

return 0;

}