Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Character Handling Question

Reply
Thread Tools

Character Handling Question

 
 
Exits Funnel
Guest
Posts: n/a
 
      04-25-2004
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

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-25-2004

"Exits Funnel" <(E-Mail Removed)> wrote in message
newszHic.13357$(E-Mail Removed) et...
> 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.

> -exits
>


john


 
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
character encoding +missing character sequence raavi Java 2 03-02-2006 05:01 AM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM
warning: multi-character character constant...help me! mimmo C Programming 4 04-10-2004 08:15 PM
Character reference "&#c" is an invalid XML character cgbusch XML 6 09-02-2003 07:04 PM
question: reading character for character from stdin KwikRick Python 1 08-22-2003 05:56 PM



Advertisments