Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about string compare stricmp

Reply
Thread Tools

question about string compare stricmp

 
 
xuatla
Guest
Posts: n/a
 
      09-08-2005
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      09-08-2005
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.
 
Reply With Quote
 
 
 
 
mahurshi@gmail.com
Guest
Posts: n/a
 
      09-09-2005
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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-09-2005
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?
 
Reply With Quote
 
xuatla
Guest
Posts: n/a
 
      09-09-2005
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
>

 
Reply With Quote
 
xuatla
Guest
Posts: n/a
 
      09-09-2005
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?

 
Reply With Quote
 
=?ISO-8859-1?Q?Stefan_N=E4we?=
Guest
Posts: n/a
 
      09-09-2005
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
 
Reply With Quote
 
=?ISO-8859-1?Q?Stefan_N=E4we?=
Guest
Posts: n/a
 
      09-09-2005
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
 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      09-09-2005

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...rings.html#ga1

 
Reply With Quote
 
xuatla
Guest
Posts: n/a
 
      09-09-2005
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...rings.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
 
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
stricmp() versus strcasecmp( ahso C++ 4 11-25-2009 03:29 PM
stricmp Michael Sgier C++ 2 07-07-2006 03:32 AM
Need to implement strdup, strnicmp and stricmp jamihuq C Programming 18 06-30-2006 07:42 PM
strcmp vs. string::compare(const string &) lchian@yahoo.com C++ 10 03-03-2006 08:53 PM
comparing two strcasecmp (stricmp) implementations William Krick C Programming 88 12-08-2005 11:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57