赞
踩
vim alpha_order.c
#include <stdio.h> #include <string.h> int main(void) { int i, j, num; char name[20][20], t_name[15][10], temp[20]; printf("Please enter how many number of names to be sorted in alphabetical order.\n"); scanf("%d", &num); printf("Please enter %d names one by one\n", num); for (i = 0; i < num; i++) { scanf("%s", name[i]); strcpy(t_name[i], name[i]); } for (i = 0; i < num - 1; i++) { for (j = i + 1; j < num; j++) { if(strcmp(name[i], name[j]) > 0) { strcpy(temp, name[i]); strcpy(name[i], name[j]); strcpy(name[j], temp); } } } printf("Names before sorting in alphaetical order\n"); for (i = 0; i < num; i++) { printf("%s ", t_name[i]); } printf("\n"); printf("Names after sorting in alphaetical order:\n"); for (i = 0; i < num; i++) { printf("%s ", name[i]); } printf("\n"); return 0; }
Please enter how many number of names to be sorted in alphabetical order.
5
Please enter 5 names one by one
one two three four five
Names before sorting in alphaetical order
one two three four five
Names after sorting in alphaetical order:
five four one three two
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。