Detail string arrays in the C language
- 2020-06-19 11:19:10
- OfStack
In C, string arrays can be used: char a[] [10]; Or char * a []; said
The first representation fixes the maximum size of each string. The second has no string size limit.
#include <stdio.h>
#include <string.h>
// The function of the program is The number of months for entering Arabic numerals Export English month
int main()
{
//1 Array of strings Its subscripts represent the Arabic numerals of the English month
char *month[] = {"January","February","March","April",
"May","June","July","August","September","October",
"November","December"};
char *curMonth = month[0];
int mon = 0;
printf(" Please enter the number of months in Arabic numerals :");
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(" There is no month \n");
}else{
printf(" Current month is :%s\n",curMonth);
}
return 0;
}
conclusion