请用C语言写下列代码:使用指针数组创建一个含有月份英文名的字符串数组,并使用指向指针的指针指向这个字符串数组,实现输出数组中的指定字符串。 char *Month[]={ /*定义字符串数组*/ "January", "February", "March", "April", "May", "June", "Junly", "August", "September", "October", "November", "December" }; 程序运行结果示例: 输入:Input a number for month 7 输出:The month is:Junly 输入提示:"Input a number for month\n" 输入格式:"%d" 输出提示:"The month is:" 输出格式:"%s\n"
时间: 2023-07-05 14:32:10 浏览: 287
下面是使用指针数组创建字符串数组,并使用指向指针的指针输出指定字符串的C语言代码:
```c
#include <stdio.h>
int main()
{
char *Month[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"Jul",
"August",
"September",
"October",
"November",
"December"
};
char **p = Month; // 指向指针的指针指向字符串数组
int n;
printf("Input a number for month\n");
scanf("%d", &n);
if (n >= 1 && n <= 12) {
printf("The month is:%s\n", *(p + n - 1)); // 输出指定字符串
} else {
printf("Invalid input\n");
}
return 0;
}
```
程序运行结果示例:
```
Input a number for month
7
The month is:Jul
```
相关问题
用C语言编写程序:二位数组存储12个月的英文名,并按字典序排序 指针数组存储12个月的英文名,并按字典序排序
【二维数组实现】
```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;
}
```
编一个程序,输入月份号,输出该月的英文名,例如, 输入 2,输出 February。提示:用字符指针数组处理多个字 符串 用C语言
为了编写一个C语言程序,你可以创建一个字符指针数组,存储每个月份的英文名称,并通过用户输入的月份号来访问相应的字符串。下面是一个简单的示例:
```c
#include <stdio.h>
#include <string.h>
// 创建一个字符指针数组存储月份名称
const char* months[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
int main() {
int month_num;
// 输入月份号
printf("Enter the month number (1-12): ");
scanf("%d", &month_num);
// 检查输入是否有效
if(month_num >= 1 && month_num <= 12) {
// 输出对应的英文月份名称
printf("The English name for the month is: %s\n", months[month_num - 1]);
} else {
printf("Invalid input! Please enter a month number between 1 and 12.\n");
}
return 0;
}
```
在这个程序中,用户输入月份号,然后我们通过索引`months[month_num - 1]`获取相应的英文名称。注意我们减一是因为数组的索引从0开始,而实际月份是从1到12。
阅读全文