其他分享
首页 > 其他分享> > C语言:实验11-1-2 输出月份英文名.2021-08-06

C语言:实验11-1-2 输出月份英文名.2021-08-06

作者:互联网

实验11-1-2 输出月份英文名 (15 point(s))

本题要求实现函数,可以返回一个给定月份的英文名称。

函数接口定义:

char *getmonth( int n );

函数getmonth应返回存储了n对应的月份英文名称的字符串头指针。如果传入的参数n不是一个代表月份的数字,则返回空指针NULL。

裁判测试程序样例:

#include <stdio.h>

char *getmonth( int n );

int main()
{
    int n;
    char *s;

    scanf("%d", &n);
    s = getmonth(n);
    if ( s==NULL ) printf("wrong input!\n");
    else printf("%s\n", s);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

5

结尾无空行

输出样例1:

May

结尾无空行

输入样例2:

15

输出样例2:

wrong input!

参考答案 

char *getmonth( int n )
{
    char *months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    //返回存储了n对应的月份英文名称的字符串头指针。
    //如果传入的参数n不是一个代表月份的数字,则返回空指针NULL
    if(0<=n && 12>=n) return months[n-1];
    else return NULL;

}

标签:11,06,int,08,样例,char,NULL,getmonth,指针
来源: https://blog.csdn.net/u012336596/article/details/119462018