-1

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");
    }
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    even if it would be possible: switch (number[20]) is definitely wrong. – Peter Miehle Mar 06 '15 at 15:20
  • 3
    Just wanted to point out that `'january'` is not a string. Strings have to be in double quotes. What you have there is a [multicharacter literal](http://stackoverflow.com/q/3960954/10077). – Fred Larson Mar 06 '15 at 15:21

2 Answers2

1

You can't do that in c, switch will only work with integers, passing a pointer to it might compile but will not do what you need it to.

In your case I suppose you meant

switch (number)

and that might compile, also the multicharacter literals1 will make your code compile, but they will be evaluated to an integer which is implementation defined, so your code accidentally compiles but surely will not work as you expect it.

The best you can do is define a struct and use bsearch() like this2

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

struct mi {
   int nr;
   char *name;
} months[] = {
   { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
   { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
   { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
};

#define nr_of_months (sizeof(months)/sizeof(months[0]))

static int
compmi(const void *m1, const void *m2)
{
   struct mi *mi1 = (struct mi *) m1;
   struct mi *mi2 = (struct mi *) m2;
   return strcmp(mi1->name, mi2->name);
}

int
main(int argc, char **argv)
{
   int i;

   qsort(months, nr_of_months, sizeof(struct mi), compmi);
   for (i = 1; i < argc; i++) {
       struct mi key, *res;
       key.name = argv[i];
       res = bsearch(&key, months, nr_of_months,
                     sizeof(struct mi), compmi);
       if (res == NULL)
           printf("'%s': unknown month\n", argv[i]);
       else
           printf("%s: month #%d\n", res->name, res->nr);
   }
   exit(EXIT_SUCCESS);
}

1This link was copied from Fred Larson's comment.

2This example is from the linux manual for bseach()

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • If I remember correctly, I think you can just pass strcmp() to bsearch(), without the need of a wrapper function. – Lundin Mar 06 '15 at 15:25
  • @Lundin of course you can, but the array you are `bsearch()`ing in this example does not consist of strings but `struct`s. – Iharob Al Asimi Mar 06 '15 at 15:26
  • A good reason not to use a struct, but to keep the number in a separate lookup table. In this case the number is merely the index returned from bsearch() + 1, so there's not really a need to store it in the struct anyhow. – Lundin Mar 06 '15 at 15:29
  • 1
    @Lundin The values will be sorted, otherwise `bsearch()` wouldn't work. So the index will not correspond to the month number. – Iharob Al Asimi Mar 06 '15 at 15:32
0

You can't. At least, not in C.

Instead use

if((strcmp(number,"january")==0)
    printf("1");
else if((strcmp(number,"february")==0)
    printf("2");
//etc

Note that you need to include string.h in order to use strcmp.

Another way would be to store all the names of months in a two dimensional char array. Loop over this array and compare the input with each month's name using strcmp. If a match is found, print current index of month array+1.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • Linear iteration will be quite ineffective though, in case the number of strings is vast. Same goes for strcmp "ladders". For large amounts of data, you'll want to use binary search, given that the strings are stored in sorted order. – Lundin Mar 06 '15 at 15:30