Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing strings case insensitive 2

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Actually this is a multi-part question. I am looking for a function in the string class to compare 2 strings and ignore case. In Java there is such a function, compareToIgnoreCase(), but I cannnot find anything in the standard library for C++. So I thought maybe there would be a toUpper() and toLower() function but I also cannot find these. Surely there are similar functions somewhere in the C++ standard libs. Can someone please point me the way. Many thanks to you!


Barry
 
>> in the standard library for C++.

If your referring to the runtime library there are:
int toupper(int)
int tolower(int)

So to use them to transform a string you must loop through the characters of the array.

If your referring to the Standard Template Library there is the transform algorithm
Code:
#include <string>
#include <algorithim>
#include <locale>
using namespace std;

string s(“Hello World!”);
transform(s.begin(), s.end(), s.begin(), tolower);
cout << s.c_str() << endl;


-pete
I just can't seem to get back my IntelliSense
 
If you want a string class with case insensitive comparisons, but with original case preserved you could do like this:

#include <string>

// char_traits for case-insensitive comparison
struct ci_char_traits : public std::char_traits<char>
{
static bool eq(char c1, char c2)
{
return toupper(c1) == toupper(c2);
}
static bool ne(char c1, char c2)
{
return toupper(c1) != toupper(c2);
}
static bool lt(char c1, char c2)
{
return toupper(c1) < toupper(c2);
}

static int compare(const char* s1, const char* s2, size_t n)
{
char ch1 = *s1++;
char ch2 = *s2++;

while(n>0 && eq(ch1,ch2))
{
n--;
ch1=*s1++;
ch2=*s2++;
}

return toupper(ch1)-toupper(ch2);
}

static const char* find(const char *s, int n, char a)
{
while(n-- > 0 && toupper(*s) != toupper(a))
{
++s;
}
return s;
}

};

// cistring is just a combination of basic_string and
// ci_char_traits
typedef std::basic_string<char, ci_char_traits, std::allocator<char> > cistring;


test()
{
cistring s1(&quot;abcd&quot;);
cistring s2(&quot;AbCd&quot;);

bool b;
b = s1 == s2; // true
s1.c_str(); //Returns &quot;abcd&quot;
s2.c_str(); //Returns &quot;AbCd&quot;

}
}

 
>> C++ standard libs

_stricmp

Will, isn't that a Microsoft extension and therefore not standard?

-pete
I just can't seem to get back my IntelliSense
 
I didn't think titanandrews was intending to limit himself to the standard CRT
 
Will,

boy your a slippery one eh? [lol]

Sorry that came across as questioning you... I really was looking for validation of my understanding of it.

-pete
I just can't seem to get back my IntelliSense
 
So for platform independence, I should not use the runtime library?


Barry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top