Description
請撰寫一個程式,讓使用者輸入幾個姓名,並使用結構陣列來儲存人名,如Apple,Tom,Green,Jack,並依照字典順序排序並顯示。
Please write a program that allows the user to input several names and stores them using a structure array, such as "Apple, Tom, Green, Jack," and sorts them in alphabetical order.
Input
輸入第一行為要排序的人數 n,接下來有 n 個姓名,n 不超過 10,每個名字長度不超過 20。
The first line of input is the number of people to be sorted, denoted as n. Following that are n names. n does not exceed 10, and the length of each name does not exceed 20 characters.
Output
請輸出排序後的結果,每行一個姓名。(最後一行沒有換行)
Please output the sorted results, with each name on a separate line. (There is no newline at the end of the last line.)
Sample Input 1
4
Apple
Tom
Green
Jack
Sample Output 1
Apple
Green
Jack
Tom
Hint
請使用結構儲存資料。
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
// puts("How many strings u are going to enter?: ");
scanf("%d",&count);
// puts("Enter Strings one by one: ");
for(i=0;i<count;i++){
scanf("%s",str[i]);
}
for(i=0;i<count;i++)
for(j=i+1;j<count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
// printf("Order of Sorted Strings:");
for(i=0;i<count;i++){
printf("%s\\n",str[i]);
}
return 0;
}