Character Handling Question

Discussion in 'C++' started by Exits Funnel, Apr 25, 2004.

  1. Exits Funnel

    Exits Funnel Guest

    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
     
    Exits Funnel, Apr 25, 2004
    #1
    1. Advertising

  2. "Exits Funnel" <> wrote in message
    news:eek:zHic.13357$...
    > 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
     
    John Harrison, Apr 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,903
    Joerg Jooss
    Jan 19, 2006
  2. raavi
    Replies:
    2
    Views:
    917
    raavi
    Mar 2, 2006
  3. cgbusch
    Replies:
    6
    Views:
    7,525
    Mike Brown
    Sep 2, 2003
  4. mimmo
    Replies:
    4
    Views:
    28,024
  5. KwikRick
    Replies:
    1
    Views:
    368
    Christos TZOTZIOY Georgiou
    Aug 22, 2003
Loading...

Share This Page