代码如下所示:
#include <stdio.h> #include<string.h> int main() { char* month_name[]= { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec" };//将多个字符串放在字符数组里面 char month[10]; int i; gets(month); for (i = 0; i <12; i++) { if (strcmp(month, month_name[i]) == 0)//使用字符串比较函数,前者长返回1,相等返回0,后者长返回-1 { printf("%s是%d月份", month, i+1); break; } } if (i >= 12) { printf("输入有误,请重新输入"); } }
输出结果如下所示: