471,086 Members | 1,122 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

question about string compare stricmp

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
Sep 8 '05 #1
11 11248
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.
Sep 9 '05 #2
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 '05 #3
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?
Sep 9 '05 #4
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
ma******@gmail.com 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

Sep 9 '05 #5
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?

Sep 9 '05 #6
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
Sep 9 '05 #7
Stefan Nwe 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
Sep 9 '05 #8

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

Sep 9 '05 #9
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
Sep 9 '05 #10
Am 09.09.2005, 03:26 Uhr, schrieb xuatla <xu****@gmail.com>:
Thank you.

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


Then you use stricmp().
---
Volker
Sep 12 '05 #11
"xuatla" <xu****@gmail.com> 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
Sep 26 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Richard Lee | last post: by
28 posts views Thread by JKop | last post: by
2 posts views Thread by Flzw | last post: by
88 posts views Thread by William Krick | last post: by
18 posts views Thread by jamihuq | last post: by
2 posts views Thread by Michael Sgier | last post: by
3 posts views Thread by > Adrian | last post: by
4 posts views Thread by sagi | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.