Character Handling Question

E

Exits Funnel

Hello,

I've two quick questions. Suppose I've got the following:

char* cptr = "This is potentially, a really, really long string";

I need to determine if the string contains "PULP FICTION" without regard
to case. For example, all of "PULP FICTION", "pulp fiction", and PuLp
Fiction" meet my criteria. I don't care WHERE in the character array it
occurs, I just want to know if it's there. Is there a standard library
function which will do this for me or do I need to write something
myself? Either something which will handle the char* directly or
somethign in std::string; either would be okay. I've poked around a
fair bit and can't find anyting, but it seems this should be a pretty
common kind of thing to do.

On a related note: If I follow the line above with this:

std::string myString = cptr;

does this involve copying the entire contents of cptr, or does the
automatic myString implement copy on write semantics? I suspect the
former but it couldn't hurt to ask. Thanks in advance for any replies!

-exits
 
J

John Harrison

Exits Funnel said:
Hello,

I've two quick questions. Suppose I've got the following:

char* cptr = "This is potentially, a really, really long string";

I need to determine if the string contains "PULP FICTION" without regard
to case. For example, all of "PULP FICTION", "pulp fiction", and PuLp
Fiction" meet my criteria. I don't care WHERE in the character array it
occurs, I just want to know if it's there. Is there a standard library
function which will do this for me or do I need to write something
myself? Either something which will handle the char* directly or
somethign in std::string; either would be okay. I've poked around a
fair bit and can't find anyting, but it seems this should be a pretty
common kind of thing to do.

Its possible to write a case insensitive string class by createing a custom
char_traits class.

class CaseInsensitveCharTraits : public std::char_traits<char>
{
...
};

typedef std::basic_string<char, CaseInsensitveCharTraits>
CaseInsensitiveString;

You can look up the details in The C++ Standard Library by Josuttis. Once
you done that all the regular string handling functions will work in a case
insensitive manner, no need for special functions. So you could just write

CaseInsensitiveString x = "This is potentially, a really, really long
string";
CaseInsensitiveString y = "pulp fiction";
if (x.find(y) != CaseInsensitiveString::npos)
{
...
}

This is definitely worth doing (and learning about) if you plan to do a lot
of case insenstive string handling.
On a related note: If I follow the line above with this:

std::string myString = cptr;

does this involve copying the entire contents of cptr, or does the
automatic myString implement copy on write semantics? I suspect the
former but it couldn't hurt to ask. Thanks in advance for any replies!

The former.

string may implement copy on write in general, but it can't do in the
example you quote because it can't 'take ownership' of a char*. What if the
char* was later deleted? The string would end up pointing at deleted memory.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top