Matriz de cadenas de lenguaje C

En el lenguaje C, se pueden usar matrices de cadenas:  char a[] [10]; o  char *a[]; representación La
primera representación fija el tamaño máximo de cada cadena. El segundo tipo no tiene un límite de tamaño de cadena.

#include <stdio.h>
#include <string.h>

//该程序的功能是 输入阿拉伯数字的月份数 输出英文月份
int main()
{
    //一个字符串数组 它的下标代表英文月份的阿拉伯数字 
    char *month[] = {"January","February","March","April",
            "May","June","July","August","September","October",
            "November","December"};
    char *curMonth = month[0];
    int mon = 0;
    printf("请输入阿拉伯数字的月份数:");
    scanf("%d",&mon);
    switch(mon){
        case 1: curMonth = month[0];    break;
        case 2: curMonth = month[1];    break;
        case 3: curMonth = month[2];    break;
        case 4: curMonth = month[3];    break;
        case 5: curMonth = month[4];    break;
        case 6: curMonth = month[5];    break;
        case 7: curMonth = month[6];    break;
        case 8: curMonth = month[7];    break;
        case 9: curMonth = month[8];    break;
        case 10: curMonth = month[9];   break;
        case 11: curMonth = month[10];  break;
        case 12: curMonth = month[11];  break; 
        default : curMonth = "No this month"; 
    }
    if( strcmp(curMonth,"No this month") == 0 ){
        printf("没有这个月份\n");
    }else{
        printf("当前月份为:%s\n",curMonth);
    }

    return 0;
}

 

Supongo que te gusta

Origin blog.csdn.net/weixin_38293850/article/details/112553020
Recomendado
Clasificación