Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ strings and strchr()

Reply
Thread Tools

C++ strings and strchr()

 
 
ataru@nospam.cyberspace.org
Guest
Posts: n/a
 
      08-21-2003
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 |
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      08-21-2003

<(E-Mail Removed)> wrote in message news:bi2uab$o8g$(E-Mail Removed)...

> 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';



 
Reply With Quote
 
 
 
 
ataru@nospam.cyberspace.org
Guest
Posts: n/a
 
      08-21-2003
Ron Natalie <(E-Mail Removed)> 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 |
 
Reply With Quote
 
Agent Mulder
Guest
Posts: n/a
 
      08-21-2003
http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-21-2003

<(E-Mail Removed)> wrote in message
news:bi2vmn$o8t$(E-Mail Removed)...
> Ron Natalie <(E-Mail Removed)> 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



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-21-2003

Agent Mulder <(E-Mail Removed)> wrote in message
news:bi30l0$nf1$(E-Mail Removed)1.nb.home.nl...
> (E-Mail Removed):
> > 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:perator[]()'.

$.02,
-Mike



 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-21-2003

<(E-Mail Removed)> wrote in message news:bi2vmn$o8t$(E-Mail Removed)...
> Ron Natalie <(E-Mail Removed)> 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.


 
Reply With Quote
 
ataru@nospam.cyberspace.org
Guest
Posts: n/a
 
      08-21-2003
Mike Wahler <(E-Mail Removed)> 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 |
 
Reply With Quote
 
Agent Mulder
Guest
Posts: n/a
 
      08-21-2003
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




 
Reply With Quote
 
ataru@nospam.cyberspace.org
Guest
Posts: n/a
 
      08-21-2003
Mike Wahler <(E-Mail Removed)> 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 |
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments