C++ strings and strchr()

Discussion in 'C++' started by ataru@nospam.cyberspace.org, Aug 21, 2003.

  1. Guest

    I want to find the position of a character in a string and replace it another
    if it is actually there, and I'd like the operation to be efficient. I'm
    assuming the "standard" way to do this is something like

    string s = "blah";
    int i = s.find_first_of('a'); /* returns 2, I presume? */
    s.replace(i,0,"e");

    Right? Is this the best way (assuming I haven't made some stupid error)? Is
    there a way to (easily!) get a character pointer from a C++ string so I can
    just change that one character, like maybe

    char *cp;

    if( (cp=strchr(s.c_str(), 'a') != NULL)
    *cp='e';

    ? If neither way is good, what's a better way?

    (if this is a FAQ, I didn't see it...)

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
    , Aug 21, 2003
    #1
    1. Advertising

  2. Ron Natalie Guest

    <> wrote in message news:bi2uab$o8g$...

    > string s = "blah";


    > int i = s.find_first_of('a'); /* returns 2, I presume? */


    You need to check to see if the find succeeds (unless you are sure it won't fail).
    It might return npos. You could also use s.find('a'). Since your match string is
    only a single character they both do the same thing (I'm not sure that one would
    be faster than the other).

    > s.replace(i,0,"e");


    That's a no op. It says to replace zero chars. You want.
    s.replace(i, 1, "e");
    Actually, for a single character this would be just as good
    s = 'e';
    Ron Natalie, Aug 21, 2003
    #2
    1. Advertising

  3. Guest

    Ron Natalie <> broke the eternal silence and spoke thus:

    > Actually, for a single character this would be just as good
    > s = 'e';


    You can reference strings as though they were C-style char*'s?


    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
    , Aug 21, 2003
    #3
  4. Agent Mulder Guest

    :
    > I want to find the position of a character in a string and replace it

    another
    > if it is actually there, and I'd like the operation to be efficient. I'm
    > assuming the "standard" way to do this is something like
    >
    > string s = "blah";
    > int i = s.find_first_of('a'); /* returns 2, I presume? */
    > s.replace(i,0,"e");


    #include<algorithm>
    replace(s.begin(),s.end(),'a','e');

    -X
    Agent Mulder, Aug 21, 2003
    #4
  5. Mike Wahler Guest

    <> wrote in message
    news:bi2vmn$o8t$...
    > Ron Natalie <> broke the eternal silence and spoke thus:
    >
    > > Actually, for a single character this would be just as good
    > > s = 'e';

    >
    > You can reference strings as though they were C-style char*'s?


    Not entirely. E.g. 'strlen()' et al won't necessarily work
    as you expect. But std::string does feature 'index'
    operators '[]'. It also has other 'intuitive' operators,
    such as '+' for concatenation.

    Which C++ book(s) are you reading?

    -Mike
    Mike Wahler, Aug 21, 2003
    #5
  6. Mike Wahler Guest

    Agent Mulder <> wrote in message
    news:bi30l0$nf1$1.nb.home.nl...
    > :
    > > I want to find the position of a character in a string and replace it

    > another
    > > if it is actually there, and I'd like the operation to be efficient.

    I'm
    > > assuming the "standard" way to do this is something like
    > >
    > > string s = "blah";
    > > int i = s.find_first_of('a'); /* returns 2, I presume? */
    > > s.replace(i,0,"e");

    >
    > #include<algorithm>
    > replace(s.begin(),s.end(),'a','e');


    Nearly all C++ literature I've read states that one
    should usually prefer a member function over a 'free'
    function, if it does what you need. This is because
    a member function could be optimized to take advantage
    of internal details not available to 'free' functions.

    In OP's case, I'd use 'std::string::find()' followed
    by access to the found element with 'std::string::eek:perator[]()'.

    $.02,
    -Mike
    Mike Wahler, Aug 21, 2003
    #6
  7. Ron Natalie Guest

    <> wrote in message news:bi2vmn$o8t$...
    > Ron Natalie <> broke the eternal silence and spoke thus:
    >
    > > Actually, for a single character this would be just as good
    > > s = 'e';

    >
    > You can reference strings as though they were C-style char*'s?


    Well, not totally, but they do support operator[] as shown above.
    Ron Natalie, Aug 21, 2003
    #7
  8. Guest

    Mike Wahler <> broke the eternal silence and spoke thus:

    > Which C++ book(s) are you reading?


    "The C++ Programming Language" by Stroustrup (aka God?) - it's in there, but
    the book is so big I overlooked it... sorry... but I do have a question
    about how they're implemented. Why can't you effectively modify the char*
    that c_str gives you? Wouldn't that make one's life easier?

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
    , Aug 21, 2003
    #8
  9. Agent Mulder Guest

    MW> Nearly all C++ literature I've read states that one
    MW> should usually prefer a member function over a 'free'
    MW> function, if it does what you need. This is because
    MW> a member function could be optimized to take advantage
    MW> of internal details not available to 'free' functions.

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

    <mikes>
    #include <iostream>
    #include <string>

    int main()
    {
    his();
    std::string s("blah");
    std::cout << s << '\n';
    std::string::size_type i(s.find('a'));

    if(i != std::string::npos)
    s = 'e';

    std::cout << s << '\n';

    return 0;
    }
    </mikes>

    -X
    Agent Mulder, Aug 21, 2003
    #9
  10. Guest

    Mike Wahler <> broke the eternal silence and spoke thus:

    > Which C++ book(s) are you reading?


    "The C++ Programming Language" by Stroustrup (aka God?) - it's in there, but
    the book is so big I overlooked it... sorry... but I do have a question
    about how they're implemented. Why can't you effectively modify the char*
    that c_str gives you? Wouldn't that make one's life easier when dealing with
    C-style string functions?

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
    , Aug 21, 2003
    #10
  11. Bob Jacobs Guest

    "Mike Wahler" <> wrote in message
    news:l881b.1969$...
    >
    > Agent Mulder <> wrote in message
    > news:bi30l0$nf1$1.nb.home.nl...
    > > :
    > > > I want to find the position of a character in a string and replace it

    > > another
    > > > if it is actually there, and I'd like the operation to be efficient.

    > I'm
    > > > assuming the "standard" way to do this is something like
    > > >
    > > > string s = "blah";
    > > > int i = s.find_first_of('a'); /* returns 2, I presume? */
    > > > s.replace(i,0,"e");

    > >
    > > #include<algorithm>
    > > replace(s.begin(),s.end(),'a','e');

    >
    > Nearly all C++ literature I've read states that one
    > should usually prefer a member function over a 'free'
    > function, if it does what you need. This is because
    > a member function could be optimized to take advantage
    > of internal details not available to 'free' functions.
    >
    > In OP's case, I'd use 'std::string::find()' followed
    > by access to the found element with 'std::string::eek:perator[]()'.


    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.
    Bob Jacobs, Aug 21, 2003
    #11
  12. Mike Wahler Guest

    Agent Mulder <> wrote in message
    news:bi3347$82h$1.nb.home.nl...
    > MW> Nearly all C++ literature I've read states that one
    > MW> should usually prefer a member function over a 'free'
    > MW> function, if it does what you need. This is because
    > MW> a member function could be optimized to take advantage
    > MW> of internal details not available to 'free' functions.
    >
    > You produced some very ugly code, Mike ;-)7


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

    -Mike
    Mike Wahler, Aug 21, 2003
    #12
  13. Guest

    Bob Jacobs <> broke the eternal silence and spoke thus:

    > 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.


    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.

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
    , Aug 21, 2003
    #13
  14. Mike Wahler Guest

    <> wrote in message
    news:bi33hk$oik$...
    > Mike Wahler <> broke the eternal silence and spoke

    thus:
    >
    > > Which C++ book(s) are you reading?

    >
    > "The C++ Programming Language" by Stroustrup (aka God?) - it's in there,

    but
    > the book is so big I overlooked it... sorry... but I do have a question
    > about how they're implemented. Why can't you effectively modify the char*
    > that c_str gives you? Wouldn't that make one's life easier when dealing

    with
    > C-style string functions?


    Why do you want to use those functions with std::string's?

    AFAIK, std::string supports anything you can do with a 'C-style
    string', and more, in a much safer way.

    -Mike

    >
    > --
    > Christopher Benson-Manica | Jumonji giri, for honour.
    > ataru(at)cyberspace.org |
    Mike Wahler, Aug 21, 2003
    #14
  15. wrote:
    > Ron Natalie <> broke the eternal silence and spoke thus:
    >
    >
    >>Actually, for a single character this would be just as good
    >> s = 'e';

    >
    >
    > You can reference strings as though they were C-style char*'s?
    >
    >


    You can't access the string directly as C-style char*'s,
    but you _can_ access a copy that is a C-style string.
    Look up the "c_str()" method:
    string b("blah");
    cout << strlen(b.c_str());

    The std::string class has many of the functions that
    are used on C-style strings. Look through the list
    of std::string methods.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 21, 2003
    #15
  16. Ron Natalie Guest

    "Mike Wahler" <> wrote in message news:Tu81b.1985
    > 2) Even if modifying this array were allowed, it is *not*
    > the characters of the string itself, only a copy.


    Well, potentially not. In practice, it usually is.
    Ron Natalie, Aug 21, 2003
    #16
  17. Ron Natalie Guest

    <> wrote in message news:bi347n$ole$...
    > Bob Jacobs <> broke the eternal silence and spoke thus:
    >
    > > 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.

    >
    > 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.


    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.
    Ron Natalie, Aug 21, 2003
    #17
  18. Default User Guest

    wrote:
    >
    > Ron Natalie <> broke the eternal silence and spoke thus:
    >
    > > Actually, for a single character this would be just as good
    > > s = 'e';

    >
    > You can reference strings as though they were C-style char*'s?



    Yes. What book are you using?



    Brian Rodenborn
    Default User, Aug 21, 2003
    #18
  19. Guest

    Mike Wahler <> broke the eternal silence and spoke thus:

    > AFAIK, std::string supports anything you can do with a 'C-style
    > string', and more, in a much safer way.


    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)cyberspace.org |
    , Aug 21, 2003
    #19
  20. Guest

    Mike Wahler <> broke the eternal silence and spoke thus:

    > 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.


    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)cyberspace.org |
    , Aug 21, 2003
    #20
    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. Martin Dickopp

    Re: Question about strchr() / strrchr()

    Martin Dickopp, Aug 16, 2003, in forum: C Programming
    Replies:
    4
    Views:
    2,241
    Irrwahn Grausewitz
    Aug 16, 2003
  2. Servé Lau

    a shorter strchr

    Servé Lau, Feb 17, 2004, in forum: C Programming
    Replies:
    9
    Views:
    666
  3. David Warner

    strchr & finding multiple occurances of a char

    David Warner, Apr 25, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,351
    CBFalconer
    Apr 26, 2005
  4. ts

    strchr declaration

    ts, Mar 14, 2007, in forum: C Programming
    Replies:
    2
    Views:
    399
    Florian Weingarten
    Mar 14, 2007
  5. earthling

    strchr

    earthling, Nov 15, 2008, in forum: C Programming
    Replies:
    2
    Views:
    443
    earthling
    Nov 16, 2008
Loading...

Share This Page