switch on strings

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

    #1

    switch on strings

    how to implement a switch case for stringse...swit ch should happen
    depending on the strings...

    thanks
    sinbad

  • user923005

    #2
    Re: switch on strings

    On Sep 24, 11:19 pm, sinbad <sinbad.sin...@ gmail.comwrote:
    how to implement a switch case for stringse...swit ch should happen
    depending on the strings...
    Suggestion: do a web search for "perfect hash".
    >From the C FAQ:
    20.17: Is there a way to switch on strings?

    A: Not directly. Sometimes, it's appropriate to use a separate
    function to map strings to integer codes, and then switch on
    those. Otherwise, of course, you can fall back on strcmp()
    and
    a conventional if/else chain. See also questions 10.12,
    20.18,
    and 20.29.

    References: K&R1 Sec. 3.4 p. 55; K&R2 Sec. 3.4 p. 58; ISO
    Sec. 6.6.4.2; H&S Sec. 8.7 p. 248.

    Comment

    • Martin Ambuhl

      #3
      Re: switch on strings

      sinbad wrote:
      how to implement a switch case for stringse...swit ch should happen
      depending on the strings...
      The control expression in a switch startement must be of an integral
      type. Strings are not of integral type.

      What you need to do is to map the string onto an integer and use that
      integer as the control expression in the switch statement.

      Comment

      • karthikbalaguru

        #4
        Re: switch on strings

        On Sep 25, 11:49 am, Martin Ambuhl <mamb...@earthl ink.netwrote:
        sinbad wrote:
        how to implement a switch case for stringse...swit ch should happen
        depending on the strings...
        >
        The control expression in a switch startement must be of an integral
        type. Strings are not of integral type.
        >
        What you need to do is to map the string onto an integer and use that
        integer as the control expression in the switch statement.
        Mapping will help u.

        Karthik Balaguru

        Comment

        • michel.gareil@gmail.com

          #5
          Re: switch on strings

          Hi
          may you can emulate a switch structure with a do while... something
          like that :

          do
          {

          /* first case : */
          if( strcmp(someStri ng, refString ) == 0 )
          {
          /* any action */
          break ;
          }
          /* second case : */
          if( strcmp(anotherS tring, refString ) == 0 )
          {
          /* any action */
          break ;
          }

          /* here can come the default : branch */

          }while( 0 ) ;



          Hope it will help :-)

          Regards

          On 25 sep, 12:04, karthikbalaguru <karthikbalagur ...@gmail.comwr ote:
          On Sep 25, 11:49 am, Martin Ambuhl <mamb...@earthl ink.netwrote:
          >
          sinbad wrote:
          how to implement a switch case for stringse...swit ch should happen
          depending on the strings...
          >
          The control expression in a switch startement must be of an integral
          type. Strings are not of integral type.
          >
          What you need to do is to map the string onto an integer and use that
          integer as the control expression in the switch statement.
          >
          Mapping will help u.
          >
          Karthik Balaguru

          Comment

          • Jack Klein

            #6
            Re: switch on strings

            On Tue, 25 Sep 2007 15:02:20 -0000, michel.gareil@g mail.com wrote in
            comp.lang.c:
            Hi
            Hi yourself. Don't top-post here, when you write a reply your new
            material belongs AFTER the quoted material in the original post you
            are responding to.
            may you can emulate a switch structure with a do while... something
            like that :
            >
            do
            {
            >
            /* first case : */
            if( strcmp(someStri ng, refString ) == 0 )
            {
            /* any action */
            break ;
            }
            /* second case : */
            if( strcmp(anotherS tring, refString ) == 0 )
            {
            /* any action */
            break ;
            }
            >
            /* here can come the default : branch */
            >
            }while( 0 ) ;
            And you can replace the clumsy structure above with:

            if (strcmp(str1, test_str) == 0)
            {
            /* whatever */
            }
            else if (strcmp(str2,te st_str) == 0)
            {
            /* different whatever */
            }
            else
            {
            /* default for no match */
            }

            Then no dummy do ... while loop or break statements clutter up the
            readability.

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://c-faq.com/
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++

            Comment

            • Keith Thompson

              #7
              Re: switch on strings

              karthikbalaguru <karthikbalagur u79@gmail.comwr ites:
              [...]
              Mapping will help u.
              Who is "u"?

              When you post here, please don't use silly abbreviations like "u" for
              "you". This is not a chat room. We'll certainly make allowances for
              spelling and grammar, especially if English isn't your first language,
              but please take the time to spell out words (and use proper
              punctuation). Thank you.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Kenny McCormack

                #8
                Re: switch on strings

                In article <lnejgmzg4x.fsf @nuthaus.mib.or g>,
                karthikbalaguru <karthikbalagur u79@gmail.comwr ote:
                >karthikbalagur u <karthikbalagur u79@gmail.comwr ites:
                >[...]
                >Mapping will help u.
                >
                >Who is "u"?
                to which
                Keith Thompson <kst-u@mib.orgmeant to write:
                >Whn y pst hr, pls dn't s slly bbrvtns lk "" fr
                >"y". Ths s nt cht rm. W'll crtnly mk llwncs fr
                >spllng nd grmmr, spclly f Englsh sn't yr frst lngg,
                >bt pls tk th tm t spll t wrds (nd s prpr
                >pncttn). Thnk y.
                HTH. HAND.

                Comment

                • Richard Tobin

                  #9
                  Re: switch on strings

                  In article <lnejgmzg4x.fsf @nuthaus.mib.or g>,
                  Keith Thompson <kst-u@mib.orgwrote:
                  >Mapping will help u.
                  >Who is "u"?
                  Traditionally, unix systems recorded various information about each
                  process in the "u area". I imagine mapping could be very useful in
                  implementing this.

                  -- Richard
                  --
                  "Considerat ion shall be given to the need for as many as 32 characters
                  in some alphabets" - X3.4, 1963.

                  Comment

                  • karthikbalaguru

                    #10
                    Re: switch on strings

                    On Sep 26, 1:13 am, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                    In article <lnejgmzg4x.... @nuthaus.mib.or g>,
                    Keith Thompson <ks...@mib.orgw rote:
                    >
                    Mapping will help u.
                    Who is "u"?
                    >
                    Traditionally, unix systems recorded various information about each
                    process in the "u area". I imagine mapping could be very useful in
                    implementing this.
                    >
                    -- Richard
                    --
                    "Considerat ion shall be given to the need for as many as 32 characters
                    in some alphabets" - X3.4, 1963.
                    It should be 'you' and not 'u'.

                    Karthik Balaguru

                    Comment

                    • CBFalconer

                      #11
                      Re: switch on strings

                      karthikbalaguru wrote:
                      rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                      >Keith Thompson <ks...@mib.orgw rote:
                      >>
                      >>>Mapping will help u.
                      >>>
                      >>Who is "u"?
                      >>
                      >Traditionall y, unix systems recorded various information about
                      >each process in the "u area". I imagine mapping could be very
                      >useful in implementing this.
                      >
                      It should be 'you' and not 'u'.
                      Well done. Now maybe the lesson will stick. Add to it the need
                      for propper snipping, especially of sigs.

                      --
                      Chuck F (cbfalconer at maineline dot net)
                      Available for consulting/temporary embedded and systems.
                      <http://cbfalconer.home .att.net>



                      --
                      Posted via a free Usenet account from http://www.teranews.com

                      Comment

                      • Richard

                        #12
                        Re: switch on strings

                        CBFalconer <cbfalconer@yah oo.comwrites:
                        karthikbalaguru wrote:
                        >rich...@cogsci .ed.ac.uk (Richard Tobin) wrote:
                        >>Keith Thompson <ks...@mib.orgw rote:
                        >>>
                        >>>>Mapping will help u.
                        >>>>
                        >>>Who is "u"?
                        >>>
                        >>Traditionally , unix systems recorded various information about
                        >>each process in the "u area". I imagine mapping could be very
                        >>useful in implementing this.
                        >>
                        >It should be 'you' and not 'u'.
                        >
                        Well done. Now maybe the lesson will stick. Add to it the need
                        for propper snipping, especially of sigs.
                        >
                        --
                        Chuck F (cbfalconer at maineline dot net)
                        Available for consulting/temporary embedded and systems.
                        <http://cbfalconer.home .att.net>
                        If you posted correctly then you could talk about signature
                        snipping. What *is* wrong with you?

                        I can't be arsed to manually cut your *first* signature off when I have
                        an automatic method which works for everyone else who posts to usenet
                        standards with a single signature. The fact that teranews adds another
                        sig is neither here nor there. There are multiple other FREE news
                        servers you could use which do not add this PITA second signature.

                        Please do not net nanny until you see fit to apply the same rules to
                        yourself.

                        Comment

                        • Keith Thompson

                          #13
                          Re: switch on strings

                          Richard <rgrdev@gmail.c omwrites:
                          CBFalconer <cbfalconer@yah oo.comwrites:
                          [...]
                          >Well done. Now maybe the lesson will stick. Add to it the need
                          >for propper snipping, especially of sigs.
                          >>
                          >--
                          > Chuck F (cbfalconer at maineline dot net)
                          > Available for consulting/temporary embedded and systems.
                          > <http://cbfalconer.home .att.net>
                          >
                          If you posted correctly then you could talk about signature
                          snipping. What *is* wrong with you?
                          [...]

                          Please don't quote signatures. If your newsreader doesn't properly
                          snip them for you, then *do it yourself*. Is manually deleting four
                          or so lines of text too difficult for you, when you generally should
                          be manually deleting large chunks of the quoted parent article anyway?
                          Sheesh.

                          Yes, Chuck's signature is longer than it should be. Yes, the fact
                          that his signature has an additional "-- " line within it causes minor
                          problems for some software, problems that can easily be worked around.
                          The point has been made.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • Richard

                            #14
                            Re: switch on strings

                            Keith Thompson <kst-u@mib.orgwrites :
                            Richard <rgrdev@gmail.c omwrites:
                            >CBFalconer <cbfalconer@yah oo.comwrites:
                            [...]
                            >>Well done. Now maybe the lesson will stick. Add to it the need
                            >>for propper snipping, especially of sigs.
                            >>>
                            >>--
                            >> Chuck F (cbfalconer at maineline dot net)
                            >> Available for consulting/temporary embedded and systems.
                            >> <http://cbfalconer.home .att.net>
                            >>
                            >If you posted correctly then you could talk about signature
                            >snipping. What *is* wrong with you?
                            [...]
                            >
                            Please don't quote signatures. If your newsreader doesn't properly
                            snip them for you, then *do it yourself*. Is manually deleting four
                            or so lines of text too difficult for you, when you generally should
                            be manually deleting large chunks of the quoted parent article anyway?
                            Sheesh.
                            >
                            Yes, Chuck's signature is longer than it should be. Yes, the fact
                            I don't care about that. The fact is he breaks the "commons sense" rules
                            with his double sig and it breaks Gnus, SLRN to name but two. his sig
                            could be 1000 lines long for all I care - so long as it is correctly
                            delimited. Yes, i read all that baloney about how the RFC says this and
                            that and how news readers "should" be able to cope with it. But to
                            repeat myself - it is him and him ONLY who breaks this common sense
                            rule of having ONE "-- " clause.
                            that his signature has an additional "-- " line within it causes minor
                            problems for some software, problems that can easily be worked around.
                            No, it can not be "easily" worked around. Only manually in my case, and
                            I am not rewriting the elisp in one of the most powerful, oldest
                            newsreaders in existance because Mr CBFalconer sees fit to flaunt the
                            standards. Maybe that makes me as bad as him. I don't know and I don't
                            care.
                            The point has been made.
                            Sorry. No. "Chuck" repeatedly refuses to live by the rules he set. or to
                            listen to anyone. There are oodles of free alternatives to teranews. The
                            fact that he even commented on the previous poster needing to learn how
                            to snip signatures is just too much.

                            Possibly because it his him that I am so annoyed. He is constantly
                            whining and lecturing people on petty posting issues and yet thinks he
                            himself is above his own rules.

                            Yes, I am being petty. I don't necessarily like killfiles as I find them
                            the resort of the weak and small minded.

                            Comment

                            • Old Wolf

                              #15
                              Re: switch on strings

                              On Sep 27, 12:08 am, CBFalconer <cbfalco...@yah oo.comwrote:
                              Well done. Now maybe the lesson will stick. Add to it the need
                              for propper snipping, especially of sigs.
                              >
                              --
                              Chuck F (cbfalconer at maineline dot net)
                              Available for consulting/temporary embedded and systems.
                              <http://cbfalconer.home .att.net>
                              >
                              --
                              Posted via a free Usenet account fromhttp://www.teranews.co m
                              Physician, heal thyself..

                              Comment

                              Working...