C++ strings and strchr()

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

    #16
    Re: C++ strings and strchr()


    "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:Tu81b.1985[color=blue]
    > 2) Even if modifying this array were allowed, it is *not*
    > the characters of the string itself, only a copy.[/color]

    Well, potentially not. In practice, it usually is.


    Comment

    • Ron Natalie

      #17
      Re: C++ strings and strchr()


      <ataru@nospam.c yberspace.org> wrote in message news:bi347n$ole $1@chessie.cirr .com...[color=blue]
      > Bob Jacobs <news05@robertj acobs.fsnet.co. uk> broke the eternal silence and spoke thus:
      >[color=green]
      > > It would be useful to know the OP's intention here, as the two examples
      > > aren't completely equivalent. One is intended to replace the first occurence
      > > of 'a' with 'e' whilst the other will replace all instances of 'a' with 'e'.
      > > For "blah" the effect is the same, of course.[/color]
      >
      > My original (flawed!) intention was actually to replace a single character in
      > a string with a null terminator (a la C - guess what I took in college? ;)),
      > which of course wouldn't have worked... And then I realized that the strings
      > I wanted already existed in the code anyway, so the whole exercise was
      > pointless... but at least I learned something.[/color]

      Did you really want to poke a null terminator there, or shorten the string.
      Putting a null terminator in a C++ string doesn't make it any shorter.


      Comment

      • ataru@nospam.cyberspace.org

        #18
        Re: C++ strings and strchr()

        Mike Wahler <mkwahler@mkwah ler.net> broke the eternal silence and spoke thus:
        [color=blue]
        > AFAIK, std::string supports anything you can do with a 'C-style
        > string', and more, in a much safer way.[/color]

        I guess I'm still dwelling in pointer-land... oh well. Actually, did C++
        come up with a better strtok()?

        --
        Christopher Benson-Manica | Jumonji giri, for honour.
        ataru(at)cybers pace.org |

        Comment

        • ataru@nospam.cyberspace.org

          #19
          Re: C++ strings and strchr()

          Mike Wahler <mkwahler@mkwah ler.net> broke the eternal silence and spoke thus:
          [color=blue]
          > 1) c_str() returns a pointer to an array of *const* characters.
          > 2) Even if modifying this array were allowed, it is *not*
          > the characters of the string itself, only a copy.[/color]

          Yes, but my question is why? Are they that afraid of someone actually getting
          a char* with strchr() or something and modifying it? Or is it just to
          discourage C-style thinking?

          --
          Christopher Benson-Manica | Jumonji giri, for honour.
          ataru(at)cybers pace.org |

          Comment

          • Ron Natalie

            #20
            Re: C++ strings and strchr()


            <ataru@nospam.c yberspace.org> wrote in message news:bi360t$oqq $2@chessie.cirr .com...[color=blue]
            > Mike Wahler <mkwahler@mkwah ler.net> broke the eternal silence and spoke thus:
            >[color=green]
            > > AFAIK, std::string supports anything you can do with a 'C-style
            > > string', and more, in a much safer way.[/color]
            >
            > I guess I'm still dwelling in pointer-land... oh well. Actually, did C++
            > come up with a better strtok()?
            >[/color]
            You can write one in a few lines. strtok bites because it maintains a
            hidden internal state and that it modifies it's input.


            Comment

            • Default User

              #21
              Re: C++ strings and strchr()

              ataru@nospam.cy berspace.org wrote:[color=blue]
              >
              > Ron Natalie <ron@sensor.com > broke the eternal silence and spoke thus:
              >[color=green]
              > > Actually, for a single character this would be just as good
              > > s[i] = 'e';[/color]
              >
              > You can reference strings as though they were C-style char*'s?[/color]


              Yes. What book are you using?



              Brian Rodenborn

              Comment

              • red floyd

                #22
                Re: C++ strings and strchr()

                Ron Natalie wrote:[color=blue]
                > <ataru@nospam.c yberspace.org> wrote in message news:bi360t$oqq $2@chessie.cirr .com...
                >[color=green]
                >>Mike Wahler <mkwahler@mkwah ler.net> broke the eternal silence and spoke thus:
                >>I guess I'm still dwelling in pointer-land... oh well. Actually, did C++
                >>come up with a better strtok()?
                >>[/color]
                >
                > You can write one in a few lines. strtok bites because it maintains a
                > hidden internal state and that it modifies it's input.[/color]

                IANALG (I am not a language guru), but I'd bet that your best bet is to write a
                "Tokenizer" class. The advantage of that over "strtok" would be that (if done right)
                each Tokenizer would have its own state, so you could have several different "parsings"
                going on simultaneously, plus you wouldn't corrupt your original string.

                Comment

                • Agent Mulder

                  #23
                  Re: C++ strings and strchr()

                  AM> You produced some very ugly code, Mike ;-)7

                  MW> Why do you find it 'ugly'? What would you do instead
                  MW> that you'd consider less 'ugly'?

                  At least you took the effort to type code. That's good.

                  #include <iostream>
                  #include <algorithm>
                  #include <string>
                  int main(int argc,char**argv )
                  {
                  std::string s("Bach");
                  std::cout<<"\nB efore: "<<s.c_str( );
                  std::replace(s. begin(),s.end() ,'B','J');
                  std::replace(s. begin(),s.end() ,'c','h');
                  std::replace(s. begin(),s.end() ,'h','z');
                  std::cout<<"\nA fter: "<<s.c_str( );
                  }
                  ______
                  output
                  Before: Bach
                  After: Jazz





                  Comment

                  • Mike Wahler

                    #24
                    Re: C++ strings and strchr()

                    <ataru@nospam.c yberspace.org> wrote in message
                    news:bi35u3$oqq $1@chessie.cirr .com...[color=blue]
                    > Ron Natalie <ron@sensor.com > broke the eternal silence and spoke thus:
                    >[color=green]
                    > > Did you really want to poke a null terminator there, or shorten the[/color][/color]
                    string.[color=blue][color=green]
                    > > Putting a null terminator in a C++ string doesn't make it any shorter.[/color]
                    >
                    > I know this now, which is why I said my original plan was flawed (because[/color]
                    I[color=blue]
                    > *did* want to shorten the string...) :)[/color]

                    #include <iostream>
                    #include <string>

                    int main()
                    {
                    std::string s("blah");
                    std::string::si ze_type pos(s.find('a') );

                    if(pos != std::string::np os)
                    s.erase(pos);

                    std::cout << s << '\n'; /* prints "bl" */
                    return 0;
                    }

                    Recommendation:


                    -Mike



                    Comment

                    • Ron Natalie

                      #25
                      Re: C++ strings and strchr()


                      "red floyd" <no.spam@here.d ude> wrote in message news:G9a1b.156$ BC4.6562829@new ssvr21.news.pro digy.com...
                      [color=blue]
                      >
                      > IANALG (I am not a language guru), but I'd bet that your best bet is to write a
                      > "Tokenizer" class. The advantage of that over "strtok" would be that (if done right)
                      > each Tokenizer would have its own state, so you could have several different "parsings"
                      > going on simultaneously, plus you wouldn't corrupt your original string.[/color]

                      Yep, and I believe if you google back a bit in this group for "stringtok" you'll find one has
                      been posted.


                      Comment

                      • Default User

                        #26
                        Re: C++ strings and strchr()

                        ataru@nospam.cy berspace.org wrote:[color=blue]
                        >
                        > Ron Natalie <ron@sensor.com > broke the eternal silence and spoke thus:
                        >[color=green]
                        > > Did you really want to poke a null terminator there, or shorten the string.
                        > > Putting a null terminator in a C++ string doesn't make it any shorter.[/color]
                        >
                        > I know this now, which is why I said my original plan was flawed (because I
                        > *did* want to shorten the string...) :)[/color]


                        If you want to use std::string, you need to sit and study them
                        thoroughly. Stop worrying about C-style strings.




                        Brian Rodenborn

                        Comment

                        • Mike Wahler

                          #27
                          Re: C++ strings and strchr()


                          Agent Mulder <mbmulder_remov e_this_@home.nl > wrote in message
                          news:bi3bcp$v8k $1@news2.tilbu1 .nb.home.nl...[color=blue]
                          > AM> You produced some very ugly code, Mike ;-)7
                          >
                          > MW> Why do you find it 'ugly'? What would you do instead
                          > MW> that you'd consider less 'ugly'?
                          >
                          > At least you took the effort to type code. That's good.
                          >
                          > #include <iostream>
                          > #include <algorithm>
                          > #include <string>
                          > int main(int argc,char**argv )
                          > {
                          > std::string s("Bach");
                          > std::cout<<"\nB efore: "<<s.c_str( );
                          > std::replace(s. begin(),s.end() ,'B','J');
                          > std::replace(s. begin(),s.end() ,'c','h');
                          > std::replace(s. begin(),s.end() ,'h','z');
                          > std::cout<<"\nA fter: "<<s.c_str( );
                          > }[/color]

                          Why is my code 'uglier' than yours?

                          I find yours 'uglier' if for no other reason than
                          that you failed to indent. You also declared parameters
                          to main() which you never use. Yuck! :-)

                          Why do you make the unnecessary call to 'c_str()'
                          for output?



                          (My computer can beat up your computer! :-) )

                          -Mike



                          Comment

                          • Default User

                            #28
                            Re: C++ strings and strchr()

                            ataru@nospam.cy berspace.org wrote:[color=blue]
                            >
                            > Mike Wahler <mkwahler@mkwah ler.net> broke the eternal silence and spoke thus:
                            >[color=green]
                            > > AFAIK, std::string supports anything you can do with a 'C-style
                            > > string', and more, in a much safer way.[/color]
                            >
                            > I guess I'm still dwelling in pointer-land... oh well. Actually, did C++
                            > come up with a better strtok()?[/color]


                            Nothing built-in. Here's a utility I wrote, it's not equivilent to
                            strtok() but rather ones like PHP explode():


                            #include <vector>
                            #include <string>

                            // breaks apart a string into substrings separated by a character string
                            // does not use a strtok() style list of separator characters
                            // returns a vector of std::strings

                            std::vector<std ::string> Explode (const std::string &inString,
                            const std::string &separator)
                            {
                            std::vector<std ::string> returnVector;
                            std::string::si ze_type start = 0;
                            std::string::si ze_type end = 0;

                            while ((end=inString. find (separator, start)) != std::string::np os)
                            {
                            returnVector.pu sh_back (inString.subst r (start, end-start));
                            start = end+separator.s ize();
                            }

                            returnVector.pu sh_back (inString.subst r (start));

                            return returnVector;
                            }



                            Brian Rodenborn

                            Comment

                            • Mike Wahler

                              #29
                              Re: C++ strings and strchr()

                              Default User <first.last@com pany.com> wrote in message
                              news:3F453CC4.2 EE811CC@company .com...[color=blue]
                              > ataru@nospam.cy berspace.org wrote:[color=green]
                              > >
                              > > Mike Wahler <mkwahler@mkwah ler.net> broke the eternal silence and spoke[/color][/color]
                              thus:[color=blue][color=green]
                              > >[color=darkred]
                              > > > AFAIK, std::string supports anything you can do with a 'C-style
                              > > > string', and more, in a much safer way.[/color]
                              > >
                              > > I guess I'm still dwelling in pointer-land... oh well. Actually, did[/color][/color]
                              C++[color=blue][color=green]
                              > > come up with a better strtok()?[/color]
                              >
                              >
                              > Nothing built-in. Here's a utility I wrote, it's not equivilent to
                              > strtok() but rather ones like PHP explode():
                              >
                              >
                              > #include <vector>
                              > #include <string>
                              >
                              > // breaks apart a string into substrings separated by a character string
                              > // does not use a strtok() style list of separator characters
                              > // returns a vector of std::strings
                              >
                              > std::vector<std ::string> Explode (const std::string &inString,
                              > const std::string &separator)
                              > {
                              > std::vector<std ::string> returnVector;
                              > std::string::si ze_type start = 0;
                              > std::string::si ze_type end = 0;
                              >
                              > while ((end=inString. find (separator, start)) != std::string::np os)
                              > {
                              > returnVector.pu sh_back (inString.subst r (start, end-start));
                              > start = end+separator.s ize();
                              > }
                              >
                              > returnVector.pu sh_back (inString.subst r (start));
                              >
                              > return returnVector;
                              > }
                              >[/color]

                              I guess I'll throw mine away now. :-) I started to
                              hack one out, which was beginning to look very much
                              like this. :-)

                              Thanks.
                              -Mike



                              Comment

                              • Agent Mulder

                                #30
                                Re: C++ strings and strchr()

                                Duh, I forgot the return statement:

                                #include <string>
                                #include <iostream>
                                #include <algorithm>
                                static std::string s="Bach";
                                int main(int argc,char**argv )
                                {
                                std::cout<<"\nB efore: "<<s.c_str( );
                                std::replace(s. begin(),s.end() ,'B','J');
                                std::replace(s. begin(),s.end() ,'c','h');
                                std::replace(s. begin(),s.end() ,'h','z');
                                std::cout<<"\nA fter: "<<s.c_str( );
                                return 0;
                                }
                                ______
                                output
                                Before: Bach
                                After: Jazz




                                Comment

                                Working...