switch statement with the string being tested

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • priyanka

    #1

    switch statement with the string being tested

    Hi there,

    I had a question. Is there any way of testing a string value in a
    switch statement. I have about 50 string values that can be in a string
    variable. I tried cheking them with the if else statements but it
    looked pretty ugly and the string values to be tested is still
    increasing. The switch statement seems to be a better choice then the
    if else statement. But it seems that the switch statement accepts
    integer values as the test condition. Can anybosy help me here or give
    some idea ?

    For eg,

    switch(nameofIn stitution){
    case UofCanada: printf( " U of canada\n");
    break;
    case UofIndia: printf( " U of India\n");
    break;

    ............... ....
    ............... ..
    ............... ......

    about 50 such cases
    default: printf(" Sorry, this university is not
    listed\n");
    die();
    break;
    }

    Thank you,
    Priya

  • Bill Pursell

    #2
    Re: switch statement with the string being tested


    priyanka wrote:
    Hi there,
    >
    I had a question. Is there any way of testing a string value in a
    switch statement. I have about 50 string values that can be in a string
    variable. I tried cheking them with the if else statements but it
    looked pretty ugly and the string values to be tested is still
    increasing. The switch statement seems to be a better choice then the
    if else statement. But it seems that the switch statement accepts
    integer values as the test condition. Can anybosy help me here or give
    some idea ?
    >
    For eg,
    >
    switch(nameofIn stitution){
    case UofCanada: printf( " U of canada\n");
    break;
    case UofIndia: printf( " U of India\n");
    break;
    >
    ............... ...
    ............... .
    ............... .....
    >
    about 50 such cases
    default: printf(" Sorry, this university is not
    listed\n");
    die();
    break;
    }
    >
    You could try something like this:

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

    char *accepted_strin gs[] = {
    "string 0",
    "string 1",
    "string 2",
    };

    int
    select_str(char *s)
    {
    int i;
    for (i=0; i < sizeof accepted_string s/sizeof *accepted_strin gs;
    i++)
    if (!strcmp(s, accepted_string s[i]))
    return i;
    return -1;
    }

    int
    main(int argc, char **argv)
    {
    char *test;
    int idx;

    if (argc<2)
    test = "test";
    else
    test = argv[1];

    switch( idx = select_str(test )) {
    case 0: /* Fallthru */
    case 1: /* Fallthru */
    case 2: /* Fallthru */
    printf("selecte d %d\n", idx);
    break;
    default:
    printf("unknown string\n");
    break;
    }
    return EXIT_SUCCESS;
    }

    Comment

    • Eric Sosman

      #3
      Re: switch statement with the string being tested

      priyanka wrote:
      Hi there,
      >
      I had a question. Is there any way of testing a string value in a
      switch statement. [...]
      This is Question 20.17 in the comp.lang.c Frequently
      Asked Questions (FAQ) list



      --
      Eric Sosman
      esosman@acm-dot-org.invalid

      Comment

      • Michael Mair

        #4
        Re: switch statement with the string being tested

        Eric Sosman schrieb:
        priyanka wrote:
        >I had a question. Is there any way of testing a string value in a
        >switch statement. [...]
        >
        This is Question 20.17 in the comp.lang.c Frequently
        Asked Questions (FAQ) list
        >
        http://c-faq.com/
        @OP: Heed the above.
        If you have a high number of strings or a high number of queries
        requiring the switch, you can speed up the whole thing in a simple
        manner under certain preconditions:
        If the strings' first elements are spread out nicely over a part
        of the alphabet, one can switch over s[0] and dispatch to a check
        of the respective subsets.
        Your "U of ...." does not lend itself to this approach.
        For frequent queries over a very high number of strings, "real"
        hashing may be better -- but this is outside the scope of
        comp.lang.c.

        Cheers
        Michael
        --
        E-Mail: Mine is an /at/ gmx /dot/ de address.

        Comment

        • pete

          #5
          Re: switch statement with the string being tested

          priyanka wrote:
          switch(nameofIn stitution){
          case UofCanada: printf( " U of canada\n");
          break;
          case UofIndia: printf( " U of India\n");
          break;
          char *string[] = {" U of canada\n", " U of India\n"};

          printf(string[nameofInstituti on]);

          --
          pete

          Comment

          • Thomas Matthews

            #6
            Re: switch statement with the string being tested

            priyanka wrote:
            Hi there,
            >
            I had a question. Is there any way of testing a string value in a
            switch statement. I have about 50 string values that can be in a string
            variable. I tried cheking them with the if else statements but it
            looked pretty ugly and the string values to be tested is still
            increasing. The switch statement seems to be a better choice then the
            if else statement. But it seems that the switch statement accepts
            integer values as the test condition. Can anybosy help me here or give
            some idea ?
            >
            For eg,
            >
            switch(nameofIn stitution){
            case UofCanada: printf( " U of canada\n");
            break;
            case UofIndia: printf( " U of India\n");
            break;
            >
            ............... ...
            ............... .
            ............... .....
            >
            about 50 such cases
            default: printf(" Sorry, this university is not
            listed\n");
            die();
            break;
            }
            >
            Thank you,
            Priya
            >
            Decide which language you are writing in.
            You posted this on comp.lang.c++ as well.
            One reason for deciding is that C++ has the
            std::map which is very nice for your issue,
            but you will have to implement your own map
            or associative array in C.

            --
            Thomas Matthews

            C++ newsgroup welcome message:

            C++ Faq: http://www.parashift.com/c++-faq-lite
            C Faq: http://www.eskimo.com/~scs/c-faq/top.html
            alt.comp.lang.l earn.c-c++ faq:

            Other sites:
            http://www.josuttis.com -- C++ STL Library book
            http://www.sgi.com/tech/stl -- Standard Template Library

            Comment

            • Bill Pursell

              #7
              Re: switch statement with the string being tested


              pete wrote:
              priyanka wrote:
              >
              switch(nameofIn stitution){
              case UofCanada: printf( " U of canada\n");
              break;
              case UofIndia: printf( " U of India\n");
              break;
              >
              char *string[] = {" U of canada\n", " U of India\n"};
              >
              printf(string[nameofInstituti on]);
              This won't work if any university is named:
              "U of %foo".
              printf("%s\n", string[name]) is safer, and it
              takes the '\n' out of the names.

              Comment

              • Herbert Rosenau

                #8
                Re: switch statement with the string being tested

                On Sun, 13 Aug 2006 19:30:22 UTC, "priyanka" <priyankabhar@g mail.com>
                wrote:
                Hi there,
                >
                I had a question. Is there any way of testing a string value in a
                switch statement. I have about 50 string values that can be in a string
                variable. I tried cheking them with the if else statements but it
                looked pretty ugly and the string values to be tested is still
                increasing. The switch statement seems to be a better choice then the
                if else statement. But it seems that the switch statement accepts
                integer values as the test condition. Can anybosy help me here or give
                some idea ?
                >
                For eg,
                >
                switch(nameofIn stitution){
                case UofCanada: printf( " U of canada\n");
                break;
                case UofIndia: printf( " U of India\n");
                break;
                Won't work.

                If the functiononality you have to call for different strings are
                really different you would do the following:

                struct s_array {
                char *s; /* string to compare */
                pfnc pf; /* do work for that name */
                ....... /* some parameters related to that string */
                };


                int func(struct *a_array arr);
                typedef (*pfnc)(struct *a_array);

                int fnfoo(struct *a_array);
                int fnbar(struct *a_array);
                ......

                struct s_array array[] = {
                { "foo", fnfoo, ... },
                { "bar", fnbar, ... },
                ......
                { NULL }
                };

                ......

                struct a_array *p;
                for (p = array; p->a; p++) {
                if (!strcmp(cmpstr , p->a)) {
                return p->pf(p);
                }
                }
                return ERROR_NO_FUNC_F OUND;

                The ... are for you to fill up. Get the idea and extend it to your
                best usage.

                --
                Tschau/Bye
                Herbert

                Visit http://www.ecomstation.de the home of german eComStation
                eComStation 1.2 Deutsch ist da!

                Comment

                Working...