-
Comparing Strings
Hey,
What i want to do is Compare two Strings, but when i compare them -- i want to make it compare Lower Case and Uppercase Letters. For Example, if i wanted to compare two strings:
str1 = "Str";
str2 = "str";
I want to know how i could evaluate these two strings, but make it compare upper and lowercase letters as the same thing -- so if i compared the two strings it would say they were equal; I want it to ignore upper and lowercase letters!!
Is there a function that does this included in the Standard C/C++ Library (NOTE: I use MSVC++ 6); If there is, could somebody please give me an example on how to use it.....
If there isn't, then how could i do this...??
I hope my Question is Clear,
Cya.
-
I think theres a function in the <string> header (strupr()) or something, you'll find it on this site somewhere in the totorial section on strings.
But what would I know, I'm the guy that said 2 / 2 = square root of 2. :)
Paul
-
The function you're thinking of is toupper, defined in ctype.h IIRC. Convert your strings to upper case (or copies of them if you want to leave the originals unchanged). Then use strcmp to compare them (in string.h).
[edit]
I'm pretty sure strupr is non-standard
[/edit]
-
This question has been convered many times. A search Here and a reference page will answer you question.
-
>>I want it to ignore upper and lowercase letters!!
If you don't mind using a nonstandard function for convenience then this should work fine for you :-)
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[] = "Str";
char str2[] = "str";
if (strcmpi(str1, str2) == 0)
{
cout<<"The same!"<<endl;
}
return 0;
}
-
Alright, thanks for the Help!
I'll Create my Own Function that Converts letters to lower-case.
Cya.
-
Code:
void upperstring(std::string& convert)
{
std::transform(
convert.begin(),
convert.end(),
convert.begin(),
::toupper
);
}
void lowerstring(std::string& convert)
{
std::transform(
convert.begin(),
convert.end(),
convert.begin(),
::tolower
);
}
The STL makes it easy :D