用C语言编写程序:二位数组存储12个月的英文名,并按字典序排序 指针数组存储12个月的英文名,并按字典序排序
时间: 2023-05-30 18:04:02 浏览: 110
【二维数组实现】
```c
#include <stdio.h>
#include <string.h>
int main()
{
char months[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int i, j;
char temp[10];
//按字典序排序
for (i = 0; i < 11; i++)
{
for (j = i + 1; j < 12; j++)
{
if (strcmp(months[i], months[j]) > 0)
{
strcpy(temp, months[i]);
strcpy(months[i], months[j]);
strcpy(months[j], temp);
}
}
}
//输出排序后的结果
for (i = 0; i < 12; i++)
{
printf("%s\n", months[i]);
}
return 0;
}
```
【指针数组实现】
```c
#include <stdio.h>
#include <string.h>
int main()
{
char *months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int i, j;
char *temp;
//按字典序排序
for (i = 0; i < 11; i++)
{
for (j = i + 1; j < 12; j++)
{
if (strcmp(months[i], months[j]) > 0)
{
temp = months[i];
months[i] = months[j];
months[j] = temp;
}
}
}
//输出排序后的结果
for (i = 0; i < 12; i++)
{
printf("%s\n", months[i]);
}
return 0;
}
```
阅读全文