Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ strings and strchr() (http://www.velocityreviews.com/forums/t276963-c-strings-and-strchr.html)

ataru@nospam.cyberspace.org 08-21-2003 05:08 PM

C++ strings and strchr()
 
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 |

Ron Natalie 08-21-2003 05:23 PM

Re: C++ strings and strchr()
 

<ataru@nospam.cyberspace.org> wrote in message news:bi2uab$o8g$1@chessie.cirr.com...

> 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[i] = 'e';




ataru@nospam.cyberspace.org 08-21-2003 05:32 PM

Re: C++ strings and strchr()
 
Ron Natalie <ron@sensor.com> broke the eternal silence and spoke thus:

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


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


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

Agent Mulder 08-21-2003 05:54 PM

Re: C++ strings and strchr()
 
ataru@nospam.cyberspace.org:
> 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



Mike Wahler 08-21-2003 06:19 PM

Re: C++ strings and strchr()
 

<ataru@nospam.cyberspace.org> wrote in message
news:bi2vmn$o8t$1@chessie.cirr.com...
> Ron Natalie <ron@sensor.com> broke the eternal silence and spoke thus:
>
> > Actually, for a single character this would be just as good
> > s[i] = '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 08-21-2003 06:25 PM

Re: C++ strings and strchr()
 

Agent Mulder <mbmulder_remove_this_@home.nl> wrote in message
news:bi30l0$nf1$1@news2.tilbu1.nb.home.nl...
> ataru@nospam.cyberspace.org:
> > 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::operator[]()'.

$.02,
-Mike




Ron Natalie 08-21-2003 06:26 PM

Re: C++ strings and strchr()
 

<ataru@nospam.cyberspace.org> wrote in message news:bi2vmn$o8t$1@chessie.cirr.com...
> Ron Natalie <ron@sensor.com> broke the eternal silence and spoke thus:
>
> > Actually, for a single character this would be just as good
> > s[i] = 'e';

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


Well, not totally, but they do support operator[] as shown above.



ataru@nospam.cyberspace.org 08-21-2003 06:36 PM

Re: C++ strings and strchr()
 
Mike Wahler <mkwahler@mkwahler.net> 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 |

Agent Mulder 08-21-2003 06:36 PM

Re: C++ strings and strchr()
 
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[i] = 'e';

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

return 0;
}
</mikes>

-X





ataru@nospam.cyberspace.org 08-21-2003 06:37 PM

Re: C++ strings and strchr()
 
Mike Wahler <mkwahler@mkwahler.net> 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 |


All times are GMT. The time now is 06:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.