編一程序,輸入月份號,輸出該月的英文月名。例如,輸人3,則輸出"March" ,要求用指針數組處理
解題思路: 首先定義字符串指針數字,數組中每一個元素都存放一個字符串指針,每個指針指向不同字符串的位置。則輸入月份數字后,根據下標獲取對應月份字符串的地址即可
答案:
#include<stdio.h>
int main()
{
int month;
char* Month[12] = { "January","February","March","April","May","June",
"July","August","September","October","November","December" };
while (1) {
printf("Please enter the month: ");
scanf_s("%d", &month);
if (month < 1 && month>12) {
printf("Input error, Month should be greater than 0 and less than 12\n");
}
printf("%s\n", Month[month - 1]);
}
return 0;
}