从键盘输入一个数字n,从1月~n月将英文月份按字母顺序排序并输出。
若n不合法,输出“Illegal”
#include<stdio.h>
#include<string.h>
void sort(char month[][10], int n)
{
int i, j;
char temp[10];
for (i = 0; i < n; i++)
for (j = 0; j < n - i - 1; j++)
if (strcmp(month[j], month[j + 1]) > 0)
{
strcpy(temp, month[j]);
strcpy(month[j], month[j + 1]);
strcpy(month[j + 1], temp);
}
}
int main()
{
char month[12][10] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
int n, i;
scanf("%d", &n);
if (n < 1 || n > 12)
{
printf("Illegal");
return 0;
}
else
{
sort(month, n);
for (i = 0; i < n; i++)
{
printf("%s\n", month[i]);
}
}
return 0;
}