question about string compare stricmp

Discussion in 'C++' started by xuatla, Sep 9, 2005.

  1. xuatla

    xuatla Guest

    Hi,

    I want to compare two strings regardless of the lowercase or uppercase.
    For example, "txt" same as "TXT", "Txt", ...

    I know that there's stricmp in some c++ that can perform a lowercase
    comparison. But when I use <cstring>, I can't find such function. Do you
    know any other standard c++ function(s) can do this task?

    Thanks,
    X
     
    xuatla, Sep 9, 2005
    #1
    1. Advertising

  2. xuatla

    red floyd Guest

    xuatla wrote:
    > Hi,
    >
    > I want to compare two strings regardless of the lowercase or uppercase.
    > For example, "txt" same as "TXT", "Txt", ...
    >
    > I know that there's stricmp in some c++ that can perform a lowercase
    > comparison. But when I use <cstring>, I can't find such function. Do you
    > know any other standard c++ function(s) can do this task?
    >
    > Thanks,
    > X


    I think you have to roll your own.
     
    red floyd, Sep 9, 2005
    #2
    1. Advertising

  3. xuatla

    Guest

    how about using c_str() first and then using strcmp?
    http://www.cppreference.com/cppstring/c_str.html

    i quickly wrote something to test this myelf (hopefully this is the
    correct way to do this):

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string test1 = "abcd";
    string test2 = "AbCd";

    cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    test2.c_str()) << endl;

    test2 = "ABCD";

    cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    test2.c_str()) << endl;

    test2 = "abcd";


    cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    test2.c_str()) << endl;

    test2 = "dcba";

    cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    test2.c_str()) << endl;

    test2 = "abcdE";

    cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    test2.c_str()) << endl;

    return 0;
    }

    output
    ----------------------
    abcd AbCd 1
    abcd ABCD 1
    abcd abcd 0
    abcd dcba -1
    abcd abcdE -1
     
    , Sep 9, 2005
    #3
  4. xuatla

    Kai-Uwe Bux Guest

    xuatla wrote:

    > Hi,
    >
    > I want to compare two strings regardless of the lowercase or uppercase.
    > For example, "txt" same as "TXT", "Txt", ...
    >
    > I know that there's stricmp in some c++ that can perform a lowercase
    > comparison. But when I use <cstring>, I can't find such function. Do you
    > know any other standard c++ function(s) can do this task?
    >
    > Thanks,
    > X


    Such comparisons depend on your locale. The following code defaults to the
    global locale std::locale().

    #include <locale>
    #include <string>
    #include <iostream>

    template < typename CharIter >
    bool
    sequence_equal_to_ignoring_case ( CharIter a_from, CharIter a_to,
    CharIter b_from, CharIter b_to,
    std::locale const & loc = std::locale() ) {
    if ( std::distance( a_from, a_to )
    !=
    std::distance( b_from, b_to ) ) {
    return ( false );
    }
    for ( CharIter a_iter = a_from, b_iter = b_from;
    a_iter != a_to;
    ++ a_iter, ++b_iter ) {
    if ( std::tolower( *a_iter, loc ) != std::tolower( *b_iter, loc ) ) {
    return ( false );
    }
    }
    return ( true );
    }

    bool
    string_equal_to_ignoring_case ( std::string const & a,
    std::string const & b,
    std::locale const & loc = std::locale() ) {
    return( sequence_equal_to_ignoring_case( a.begin(), a.end(),
    b.begin(), b.end(),
    loc ) );
    }

    int main ( void ) {
    std::string a ( "Hello World!" );
    std::string b ( "hello world!" );
    std::cout << string_equal_to_ignoring_case( a, b ) << '\n';
    }


    Best

    Kai-Uwe Bux

    ps.: Isn't this in the FAQ? and should it be in the FAQ?
     
    Kai-Uwe Bux, Sep 9, 2005
    #4
  5. xuatla

    xuatla Guest

    Thank you.

    Yes for functions related with char * I use .c_str().
    But strcmp is case sensative.

    For example, comparing abcd AbCC also gives result 1. So this function
    is not good for case insensative comparison.

    I am also looking for "standard" functions as makeupper() or makelower()
    in CString in MFC. It seems that we don't have such functions in
    standard C++ library.

    Regards,
    X


    wrote:
    > how about using c_str() first and then using strcmp?
    > http://www.cppreference.com/cppstring/c_str.html
    >
    > i quickly wrote something to test this myelf (hopefully this is the
    > correct way to do this):
    >
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    > int main()
    > {
    > string test1 = "abcd";
    > string test2 = "AbCd";
    >
    > cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    > test2.c_str()) << endl;
    >
    > test2 = "ABCD";
    >
    > cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    > test2.c_str()) << endl;
    >
    > test2 = "abcd";
    >
    >
    > cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    > test2.c_str()) << endl;
    >
    > test2 = "dcba";
    >
    > cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    > test2.c_str()) << endl;
    >
    > test2 = "abcdE";
    >
    > cout << test1 << " " << test2 << " " << strcmp(test1.c_str(),
    > test2.c_str()) << endl;
    >
    > return 0;
    > }
    >
    > output
    > ----------------------
    > abcd AbCd 1
    > abcd ABCD 1
    > abcd abcd 0
    > abcd dcba -1
    > abcd abcdE -1
    >
     
    xuatla, Sep 9, 2005
    #5
  6. xuatla

    xuatla Guest

    Thank you very much!! This works well for me.

    Regards,
    X


    Kai-Uwe Bux wrote:
    > xuatla wrote:
    >
    >
    >>Hi,
    >>
    >>I want to compare two strings regardless of the lowercase or uppercase.
    >>For example, "txt" same as "TXT", "Txt", ...
    >>
    >>I know that there's stricmp in some c++ that can perform a lowercase
    >>comparison. But when I use <cstring>, I can't find such function. Do you
    >>know any other standard c++ function(s) can do this task?
    >>
    >>Thanks,
    >>X

    >
    >
    > Such comparisons depend on your locale. The following code defaults to the
    > global locale std::locale().
    >
    > #include <locale>
    > #include <string>
    > #include <iostream>
    >
    > template < typename CharIter >
    > bool
    > sequence_equal_to_ignoring_case ( CharIter a_from, CharIter a_to,
    > CharIter b_from, CharIter b_to,
    > std::locale const & loc = std::locale() ) {
    > if ( std::distance( a_from, a_to )
    > !=
    > std::distance( b_from, b_to ) ) {
    > return ( false );
    > }
    > for ( CharIter a_iter = a_from, b_iter = b_from;
    > a_iter != a_to;
    > ++ a_iter, ++b_iter ) {
    > if ( std::tolower( *a_iter, loc ) != std::tolower( *b_iter, loc ) ) {
    > return ( false );
    > }
    > }
    > return ( true );
    > }
    >
    > bool
    > string_equal_to_ignoring_case ( std::string const & a,
    > std::string const & b,
    > std::locale const & loc = std::locale() ) {
    > return( sequence_equal_to_ignoring_case( a.begin(), a.end(),
    > b.begin(), b.end(),
    > loc ) );
    > }
    >
    > int main ( void ) {
    > std::string a ( "Hello World!" );
    > std::string b ( "hello world!" );
    > std::cout << string_equal_to_ignoring_case( a, b ) << '\n';
    > }
    >
    >
    > Best
    >
    > Kai-Uwe Bux
    >
    > ps.: Isn't this in the FAQ? and should it be in the FAQ?
     
    xuatla, Sep 9, 2005
    #6
  7. Kai-Uwe Bux wrote:
    > Such comparisons depend on your locale. The following code defaults to the
    > global locale std::locale().
    >
    > #include <locale>
    > #include <string>
    > #include <iostream>
    >
    > template < typename CharIter >
    > bool
    > sequence_equal_to_ignoring_case ( CharIter a_from, CharIter a_to,
    > CharIter b_from, CharIter b_to,
    > std::locale const & loc = std::locale() ) {
    > if ( std::distance( a_from, a_to )
    > !=
    > std::distance( b_from, b_to ) ) {
    > return ( false );
    > }
    > for ( CharIter a_iter = a_from, b_iter = b_from;
    > a_iter != a_to;
    > ++ a_iter, ++b_iter ) {
    > if ( std::tolower( *a_iter, loc ) != std::tolower( *b_iter, loc ) ) {
    > return ( false );
    > }
    > }
    > return ( true );
    > }
    >
    > bool
    > string_equal_to_ignoring_case ( std::string const & a,
    > std::string const & b,
    > std::locale const & loc = std::locale() ) {
    > return( sequence_equal_to_ignoring_case( a.begin(), a.end(),
    > b.begin(), b.end(),
    > loc ) );
    > }
    >
    > int main ( void ) {
    > std::string a ( "Hello World!" );
    > std::string b ( "hello world!" );
    > std::cout << string_equal_to_ignoring_case( a, b ) << '\n';
    > }


    Any problems with this solution ??


    bool
    char_equal_icase(char a, char b, std::locale const & loc = std::locale() )
    {
    return ( std::tolower( a, loc ) ==
    std::tolower( b, loc ) );
    }

    bool
    string_equal_icase ( std::string const & a,
    std::string const & b,
    std::locale const & loc = std::locale() )
    {
    return ((a.size() == b.size()) &&
    (std::mismatch(a.begin(), a.end(), b.begin(), char_equal_icase).first == a.end()));
    }


    Stefan
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Sep 9, 2005
    #7
  8. Stefan Näwe wrote:
    > Any problems with this solution ??


    Yes!
    The locale doesn'T get passed to char_equal_icase().

    > bool
    > char_equal_icase(char a, char b, std::locale const & loc = std::locale() )
    > {
    > return ( std::tolower( a, loc ) ==
    > std::tolower( b, loc ) );
    > }
    >
    > bool
    > string_equal_icase ( std::string const & a,
    > std::string const & b,
    > std::locale const & loc = std::locale() )
    > {
    > return ((a.size() == b.size()) &&
    > (std::mismatch(a.begin(), a.end(), b.begin(), char_equal_icase).first == a.end()));
    > }


    Better:


    struct char_equal_icase
    {
    char_equal_icase(std::locale const & loc = std::locale())
    : loc_(loc)
    {
    }

    bool
    operator()(char a, char b )
    {
    return ( std::tolower( a, loc_ ) == std::tolower( b, loc_) );
    }

    private:
    std::locale const & loc_;
    };

    bool
    string_equal_icase ( std::string const & a,
    std::string const & b,
    std::locale const & loc = std::locale() )
    {
    return ((a.size() == b.size()) &&
    (std::mismatch(a.begin(), a.end(), b.begin(), char_equal_icase(loc)).first == a.end()));
    }



    Stefan
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Sep 9, 2005
    #8
  9. xuatla wrote:
    > Hi,
    >
    > I want to compare two strings regardless of the lowercase or uppercase.
    > For example, "txt" same as "TXT", "Txt", ...
    >
    > I know that there's stricmp in some c++ that can perform a lowercase
    > comparison. But when I use <cstring>, I can't find such function. Do you
    > know any other standard c++ function(s) can do this task?


    There is none.

    You can use Apache Portable Runtime for that:
    http://apr.apache.org/docs/apr/group__apr__strings.html#ga1
     
    Maxim Yegorushkin, Sep 9, 2005
    #9
  10. xuatla

    xuatla Guest

    Maxim Yegorushkin wrote:
    > xuatla wrote:
    >
    >>Hi,
    >>
    >>I want to compare two strings regardless of the lowercase or uppercase.
    >>For example, "txt" same as "TXT", "Txt", ...
    >>
    >>I know that there's stricmp in some c++ that can perform a lowercase
    >>comparison. But when I use <cstring>, I can't find such function. Do you
    >>know any other standard c++ function(s) can do this task?

    >
    >
    > There is none.
    >
    > You can use Apache Portable Runtime for that:
    > http://apr.apache.org/docs/apr/group__apr__strings.html#ga1
    >


    Thank you.

    I got from another group that
    int strcasecmp((const char *s1, const char *s2)
    works for case nonsensative comparison.

    It exists in my c++ 3.3.3 but not in visual c++. So I think this might
    not be a standard c++ function.

    I have solved this problem. Thanks to all.

    X
     
    xuatla, Sep 9, 2005
    #10
  11. Am 09.09.2005, 03:26 Uhr, schrieb xuatla <>:

    > Thank you.
    >
    > Yes for functions related with char * I use .c_str().
    > But strcmp is case sensative.
    >


    Then you use stricmp().


    ---
    Volker
     
    Volker Schneider, Sep 12, 2005
    #11
  12. "xuatla" <> wrote:

    > I want to compare two strings regardless of ... case ...



    Here's my solution that I use whenever I want non-case compare:


    // Determine whether two std::strings are non-case-sensitively "equal":
    inline bool NCS_Equal (std::string const & Bob, std::string const & Fred)
    {
    return StringToLower(Bob) == StringToLower(Fred);
    }

    // Convert string to all-lower-case:
    std::string StringToLower (std::string const & InputString)
    {
    std::string OutputString;
    std::string::const_iterator i;
    for (i = InputString.begin(); i != InputString.end(); ++i)
    {
    if (isupper(*i)) OutputString.push_back(tolower(*i));
    else OutputString.push_back(*i);
    }
    return OutputString;
    }


    Doesn't deal with locales. May possibly fail in some countries.
    But works fine for English language and ASCII encoding, which are
    all that many programmers deal with.


    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant
     
    Robbie Hatley, Sep 26, 2005
    #12
    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. William  Krick

    comparing two strcasecmp (stricmp) implementations

    William Krick, Nov 10, 2005, in forum: C Programming
    Replies:
    88
    Views:
    2,642
  2. jamihuq
    Replies:
    18
    Views:
    1,743
  3. Michael Sgier

    stricmp

    Michael Sgier, Jul 6, 2006, in forum: C++
    Replies:
    2
    Views:
    640
    Walt Stoneburner
    Jul 7, 2006
  4. ahso
    Replies:
    4
    Views:
    4,368
    BGB / cr88192
    Nov 25, 2009
  5. Lynn McGuire
    Replies:
    11
    Views:
    515
    James Kanze
    Apr 30, 2013
Loading...

Share This Page