vendredi 6 mars 2015

how can i use switch with a string input with c?

I'm new programing in C, i want to do a program that convert the months to the number of the month using switch.


For example:


Input:



"january"


output:



"1"


Here what i do:



void function(char number[]);

int main()
{
char xm[20];

printf("Month:");
scanf("%s", &xm);

function(xm);

return 0;
}

void function (char number[20])
{
switch (number[20])
{
case 'january': printf("1");
break;
case 'february': printf("2");
break;
case 'march': printf("3");
break;
default:
printf("error");
}
}




Aucun commentaire:

Enregistrer un commentaire