Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

C++ strings and strchr()

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

<(E-Mail Removed)> wrote in message news:bi360t$oqq$(E-Mail Removed)...
> Mike Wahler <(E-Mail Removed)> 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()?
>

You can write one in a few lines. strtok bites because it maintains a
hidden internal state and that it modifies it's input.


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-21-2003
Ron Natalie wrote:
> <(E-Mail Removed)> wrote in message news:bi360t$oqq$(E-Mail Removed)...
>
>>Mike Wahler <(E-Mail Removed)> 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()?
>>

>
> You can write one in a few lines. strtok bites because it maintains a
> hidden internal state and that it modifies it's input.


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.

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      08-21-2003
<(E-Mail Removed)> wrote in message
news:bi35u3$oqq$(E-Mail Removed)...
> Ron Natalie <(E-Mail Removed)> broke the eternal silence and spoke thus:
>
> > 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.

>
> I know this now, which is why I said my original plan was flawed (because

I
> *did* want to shorten the string...)


#include <iostream>
#include <string>

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

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

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

Recommendation:
http://www.josuttis.com/libbook/index.html

-Mike



 
Reply With Quote
 
Agent Mulder
Guest
Posts: n/a
 
      08-21-2003
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<<"\nBefore: "<<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<<"\nAfter: "<<s.c_str();
}
______
output
Before: Bach
After: Jazz





 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-21-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Ron Natalie <(E-Mail Removed)> broke the eternal silence and spoke thus:
>
> > 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.

>
> I know this now, which is why I said my original plan was flawed (because I
> *did* want to shorten the string...)



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




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

"red floyd" <(E-Mail Removed)> wrote in message news:G9a1b.156$(E-Mail Removed). com...

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


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


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

Agent Mulder <(E-Mail Removed)> wrote in message
news:bi3bcp$v8k$(E-Mail Removed)1.nb.home.nl...
> 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<<"\nBefore: "<<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<<"\nAfter: "<<s.c_str();
> }


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



 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-21-2003
(E-Mail Removed) wrote:
>
> Mike Wahler <(E-Mail Removed)> 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()?



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::size_type start = 0;
std::string::size_type end = 0;

while ((end=inString.find (separator, start)) != std::string::npos)
{
returnVector.push_back (inString.substr (start, end-start));
start = end+separator.size();
}

returnVector.push_back (inString.substr (start));

return returnVector;
}



Brian Rodenborn
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-21-2003
Default User <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) wrote:
> >
> > Mike Wahler <(E-Mail Removed)> 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()?

>
>
> 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::size_type start = 0;
> std::string::size_type end = 0;
>
> while ((end=inString.find (separator, start)) != std::string::npos)
> {
> returnVector.push_back (inString.substr (start, end-start));
> start = end+separator.size();
> }
>
> returnVector.push_back (inString.substr (start));
>
> return returnVector;
> }
>


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



 
Reply With Quote
 
Agent Mulder
Guest
Posts: n/a
 
      08-21-2003
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<<"\nBefore: "<<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<<"\nAfter: "<<s.c_str();
return 0;
}
______
output
Before: Bach
After: Jazz




 
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