How to avoid a lot of switch case

Discussion in 'C Programming' started by silusilusilu@gmail.com, May 30, 2008.

  1. Guest

    I wrote ,as homework, a program that displays words after key pressed.
    So, if letter 'A' is pressed, the word 'dog' appears; if letter 'B' is
    pressed 'cat' appears; if letter 'a' is pressed 'apple' appears; and
    so on...
    I wrote this program with a lot of switch case, so i want to obtain a
    smaller program (if possible)...can you help me?
    , May 30, 2008
    #1
    1. Advertising

  2. On Thu, May 29, 2008 at 11:26:34PM -0700, wrote:
    > I wrote ,as homework, a program that displays words after key pressed.
    > So, if letter 'A' is pressed, the word 'dog' appears; if letter 'B' is
    > pressed 'cat' appears; if letter 'a' is pressed 'apple' appears; and
    > so on...
    > I wrote this program with a lot of switch case, so i want to obtain a
    > smaller program (if possible)...can you help me?


    What you would like to do is to map integers to strings.

    How about an array?
    const char *my_lovely_words[UCHAR_MAX];

    Please stop reading here, try to write the program, and then you may continue.

    my_lovely_words['a']="apple";
    ....
    But I do not see how that would save you much keystrokes.

    In C99 you can have named initialisers
    const char *my_lovely_words[]={
    ['A']="dog",
    ['a']="apple",
    };

    Szabolcs
    Szabolcs Borsanyi, May 30, 2008
    #2
    1. Advertising

  3. Guest

    On May 30, 9:52 am, Szabolcs Borsanyi <> wrote:
    > On Thu, May 29, 2008 at 11:26:34PM -0700, wrote:
    > > I wrote ,as homework, a program that displays words after key pressed.
    > > So, if letter 'A' is pressed, the word 'dog' appears; if letter 'B' is
    > > pressed 'cat' appears; if letter 'a' is pressed 'apple' appears; and
    > > so on...
    > > I wrote this program with a lot of switch case, so i want to obtain a
    > > smaller program (if possible)...can you help me?

    >
    > What you would like to do is to map integers to strings.
    >
    > How about an array?
    > const char *my_lovely_words[UCHAR_MAX];
    >
    > Please stop reading here, try to write the program, and then you may continue.
    >
    > my_lovely_words['a']="apple";

    And what if 'a' has the value 4325? It would waste a lot of space.
    Here's another solution:

    const char *my_lovely_words[] = { ... };
    const char *p, str[] = "#Az";
    int c;
    ....
    if(p = strchr(str, c)) != NULL) printf("%c = %s\n", c,
    my_lovely_words[p - str]);
    , May 30, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Malkis
    Replies:
    8
    Views:
    505
    Alexander Malkis
    Apr 14, 2004
  2. Jeyakumar
    Replies:
    0
    Views:
    595
    Jeyakumar
    Jun 27, 2006
  3. Roger23
    Replies:
    2
    Views:
    983
    Roger23
    Oct 12, 2006
  4. Fei Liu
    Replies:
    13
    Views:
    927
    Neil Cerutti
    Mar 6, 2006
  5. Replies:
    6
    Views:
    709
Loading...

Share This Page