/*
主函数创建5个学生的数组,写一个排序函数,让学生按姓名从小到大排序,主函数输出排序后的结果
*/
#include <stdio.h>
#include <string.h>
struct student {
char name[22];
};
void sortName();
int main(int argc, const char * argv[]) {
// insert code here...
struct student stu[5] = {{"mike"},{"linda"},{"darin"},{"jack"},{"hanson"}};
int len = 5;
sortName (stu,len);
for (int i= 0; i< len; i++) {
printf("%s;\t",stu[i].name);
}
printf("The end!\n");
return 0;
}
void sortName(struct student getstu[],int getlen){
for (int i= 0; i< getlen-1; i++) {
for (int j= 0; j< getlen-1-i; j++) {
if (strcmp(getstu[j].name, getstu[j+1].name)> 0) {
struct student tmp = {"tmp"};
tmp = getstu[j];
getstu[j] = getstu[j+1];
getstu[j+1] = tmp;
}
}
}
} |
|