replacement for stristr - Programmer

This is a discussion on replacement for stristr - Programmer ; There is a function stricmp that compares two strings without case sensitivity. We have strstr that searches for a substring, taking case into account. Is there a function stristr? If not, how to write one? Thanks. -- +++++++++++ Siemel Naran...

+ Reply to Thread
Results 1 to 12 of 12

Thread: replacement for stristr

  1. replacement for stristr

    There is a function stricmp that compares two strings without case
    sensitivity.

    We have strstr that searches for a substring, taking case into account.

    Is there a function stristr? If not, how to write one?

    Thanks.

    --
    +++++++++++
    Siemel Naran




  2. Re: replacement for stristr

    "Siemel Naran" wrote in message
    news:%Hi%b.96313$hR.1910806@bgtnsc05-news.ops.worldnet.att.net
    > There is a function stricmp that compares two strings without case
    > sensitivity.
    >
    > We have strstr that searches for a substring, taking case into
    > account.
    >
    > Is there a function stristr? If not, how to write one?
    >
    > Thanks.



    stricmp is a non-standard (presumably Microsoft) extension (now designated
    _stricmp to indicate that fact).

    I think that you will need to convert the string to all uppercase or all
    lowercase first, and then call strstr (there are Microsoft functions for
    this --- called _strupr and _strlwr --- but beware that they will overwrite
    the original string, so you may need to first make a copy).


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)


  3. Re: replacement for stristr

    John Carson wrote:

    > "Siemel Naran" wrote in message
    > news:%Hi%b.96313$hR.1910806@bgtnsc05-news.ops.worldnet.att.net
    >
    >>There is a function stricmp that compares two strings without case
    >>sensitivity.
    >>
    >>We have strstr that searches for a substring, taking case into
    >>account.
    >>
    >>Is there a function stristr? If not, how to write one?
    >>
    >>Thanks.

    >
    >
    >
    > stricmp is a non-standard (presumably Microsoft) extension (now designated
    > _stricmp to indicate that fact).
    >
    > I think that you will need to convert the string to all uppercase or all
    > lowercase first, and then call strstr (there are Microsoft functions for
    > this --- called _strupr and _strlwr --- but beware that they will overwrite
    > the original string, so you may need to first make a copy).
    >
    >


    One could compare each character in a C-style character
    using the std::toupper or std::tolower functions to make
    sure you are comparing case insensitive:

    int my_stricmp(const char * a
    const char * b)
    {
    while (*a != '\0' && *b != '\0')
    {
    if (toupper(*a) != toupper(*b))
    return (toupper(*a) < toupper(*b)) ? -1 : 1;
    ++a;
    ++b;
    }
    if (*a == *b)
    return 0;
    return (*a == '\0') ? -1 : 1;
    }

    One could use traits and locale to create an
    uppercase only (or lowercase only) string type
    using std::basic_string<>.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c++/faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book


  4. Re: replacement for stristr

    Siemel Naran wrote:
    >
    > There is a function stricmp that compares two strings without case
    > sensitivity.
    >
    > We have strstr that searches for a substring, taking case into account.
    >
    > Is there a function stristr? If not, how to write one?



    Always a good bet is snippets.org:


    http://c.snippets.org/snip_lister.php?fname=stristr.c



    Brian Rodenborn

  5. Re: replacement for stristr

    Thomas Matthews wrote:
    >
    > John Carson wrote:


    > One could compare each character in a C-style character
    > using the std::toupper or std::tolower functions to make
    > sure you are comparing case insensitive:
    >
    > int my_stricmp(const char * a
    > const char * b)



    He wanted a case-insensitive strstr(), not strcmp().



    Brian Rodenborn

  6. Re: replacement for stristr

    "Thomas Matthews" wrote in
    message

    Thanks. I can extend this idea to write my own stristr.

    > return (toupper(*a) < toupper(*b)) ? -1 : 1;


    Out of curiosity, which is faster -- the one above or

    return toupper(*a) - toupper(*b);

    I imagine it varies platform by platform?

    --
    +++++++++++
    Siemel Naran



  7. Re: replacement for stristr

    "Default User" wrote in message
    > Siemel Naran wrote:


    > > Is there a function stristr? If not, how to write one?

    >
    > Always a good bet is snippets.org:
    > http://c.snippets.org/snip_lister.php?fname=stristr.c


    Thanks. This is a nice link. So I have to write my own? Searching in the
    standard I find there is a std::search.

    template
    truct eiq {
    bool operator()(charT c1, charT c2) const {
    return toupper(c1)==toupper(c2);
    }
    };

    const char * stristr(const char * s1, const char * s2) {
    const char * s1end = s1+strlen(s1);
    const char * out = std::search(s1, s1end, s2, s2+strlen(s2),
    eiq());
    if (out == s1end) return NULL;
    return out;
    }

    I think the calls to std::strlen may make lose efficiency because we scan
    each of the entire strings once to find the length.

    I wrote the following which seems to work, but would like confirmation if it
    is correct.

    template
    const charT * enhanced::stristr(const charT * s1, const charT *const
    s2start)
    {
    using std::toupper;

    const charT * s2startplus1 = s2start + 1u;
    const charT * s2 = s2startplus1;
    const charT c2start = toupper(*s2start);
    charT c2 = c2start;

    for ( ; ; ++s1)
    {
    const charT c1 = toupper(*s1);
    if (!c1) break;

    if (c1 == c2)
    {
    c2 = toupper(*s2);
    if (!c2) return s1-(s2-s2start-1u);
    ++s2;
    }
    else if (s2 != s2startplus1)
    {
    s2 = s2startplus1;
    c2 = c2start;
    }
    }

    return NULL;
    }


    --
    +++++++++++
    Siemel Naran



  8. Re: replacement for stristr



    Siemel Naran wrote:

    >
    > I wrote the following which seems to work, but would like confirmation if it
    > is correct.
    >


    Write a test. Things to test are what happens if either input are an
    empty string "". The substring length is greater then the string to be
    searched. That the substring "aaa" is in the middle of the string
    "baaabbb". That a false start can resync "abc" "bababcd".


  9. Re: replacement for stristr

    "lilburne" wrote in message news:c1n27p$1kpdl9$1@ID-
    > Siemel Naran wrote:


    > > I wrote the following which seems to work, but would like confirmation

    if it
    > > is correct.

    >
    > Write a test. Things to test are what happens if either input are an
    > empty string "". The substring length is greater then the string to be
    > searched. That the substring "aaa" is in the middle of the string
    > "baaabbb". That a false start can resync "abc" "bababcd".


    Thanks. Anything else you can think of?

    --
    +++++++++++
    Siemel Naran



  10. Re: replacement for stristr

    Siemel,

    Here's a version I wrote many years ago and still use. It has a lot
    of miles on it, so I'm reasonably confident it won't blow up:

    LPSTR stristr(const char *pszText, const char *pszSub)
    {
    if (!pszText || !pszSub || !*pszText || !*pszSub)
    return 0;
    int nLen(int(strlen(pszSub)));
    const char test(toupper(*pszSub));
    while (*pszText)
    {
    if (toupper(*pszText)==test)
    {
    if (strnicmp(pszText,pszSub,nLen)==0)
    return LPSTR(pszText);
    }
    pszText++;
    }
    return 0;
    }

    Good luck,
    Les Matheson
    Integral Concepts, Inc.
    http://www.ivsds.com


    "Siemel Naran" wrote in message news:...
    > "lilburne" wrote in message news:c1n27p$1kpdl9$1@ID-
    > > Siemel Naran wrote:

    >
    > > > I wrote the following which seems to work, but would like confirmation

    > if it
    > > > is correct.

    > >
    > > Write a test. Things to test are what happens if either input are an
    > > empty string "". The substring length is greater then the string to be
    > > searched. That the substring "aaa" is in the middle of the string
    > > "baaabbb". That a false start can resync "abc" "bababcd".

    >
    > Thanks. Anything else you can think of?


  11. Re: replacement for stristr

    Les Matheson wrote:
    >
    > Siemel,
    >
    > Here's a version I wrote many years ago and still use. It has a lot
    > of miles on it, so I'm reasonably confident it won't blow up:
    >
    > LPSTR stristr(const char *pszText, const char *pszSub)
    > {
    > if (!pszText || !pszSub || !*pszText || !*pszSub)
    > return 0;
    > int nLen(int(strlen(pszSub)));
    > const char test(toupper(*pszSub));
    > while (*pszText)
    > {
    > if (toupper(*pszText)==test)
    > {
    > if (strnicmp(pszText,pszSub,nLen)==0)
    > return LPSTR(pszText);
    > }
    > pszText++;
    > }
    > return 0;
    > }



    I realize that this is cross-posted to a windows newsgroup, but in
    general it's better to use standard constructs whenever possible to
    improve portability. The problem at hand can be solved without use of
    non-standard functions like strincmp() or proprietary types like LPSTR.

    Also stristr is a reserved identifier.



    Brian Rodenborn

  12. Re: replacement for stristr

    > I realize that this is cross-posted to a windows newsgroup, but in
    > general it's better to use standard constructs whenever possible to
    > improve portability. The problem at hand can be solved without use of
    > non-standard functions like strincmp() or proprietary types like LPSTR.
    >
    > Also stristr is a reserved identifier.


    Good point, and thanks for the follow-up.

    Les Matheson
    Integral Concepts, Inc.
    http://www.ivsds.com

+ Reply to Thread