One thing that I would like to do now is make the search not case sensitive, any ideas out there?
I think if I convert the camparing to upper or lower case before comparison; that will work, will it not?
Cheers!
One thing that I would like to do now is make the search not case sensitive, any ideas out there?
I think if I convert the camparing to upper or lower case before comparison; that will work, will it not?
Cheers!
If you convert both to monocase before comparing, that will work.
There is no standard case-insensitive comparison function, but most OSes have a nonstandard one of their own.
You could always implement your own version using toupper();
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Use a template that accepts any object for which operator[] can be called.
*then termination might be tricky*
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Come to think of it, I suggest you just overload the function. You could bank on std::string::operator[](this->size()) to be 0, as the standard demands for c_str() -- but the former is not always true. The obvious solution is NocaseComp(str1, str2, num), but that seems inelegent, especially if only two data types are seriously under consideration... and for such a small function.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Actually, is this ugly and slow?
Code:template<class string_type> bool strcompi(string_type & a, string_type & b) { std::string one(a); std::string two(b); std::for_each(one.begin(), one.end(), std::toupper); std::for_each(two.begin(), two.end(), std::toupper); return one == two; }
"If you tell the truth, you don't have to remember anything"
-Mark Twain
The use of std::toupper (as opposed to the global toupper) is problematic in this case, though the exact reason eludes me at the moment. (Type mismatch? I know this has been discussed before.) Josuttis suggests just defining a function that compares characters without regard for case, and then use say, equal(), e.g.,Originally Posted by CodeMonkey
We could use function object template classes instead and thus not have to resort to the different function names.Code:bool noCaseEqualChar(char lhs, char rhs) { return std::toupper(lhs) == std::toupper(rhs); } bool noCaseEqual(const std::string& lhs, const std::string& rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin(), noCaseEqualChar); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It was actually much easier than that:
Then compare them, but since I don't want to change the original data I am going to copy them into temp variables, put them in uppercase, then compare them.Code:strupr((char *)studentName.c_str()); // This is the string member of the class strupr((char *)obj.studentName); // This is the char[] member of the struct
Is there any problem with doing it this way?
Thanks everyone for you help!
Yes. The c_str() member function returns a const char* for a good reason: you are not allowed to modify the string via the pointer returned. Blatantly ignoring the const qualifier by means of a cast is just sticking your fingers in your ears to try and block out the fire alarm in the hope that the fire will not actually burn you.Originally Posted by Phyxashun
Besides that, a potential problem is that strupr() is non-standard.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm not worried about strupr not being standard.
but is this correct?
it works, and doesn't modify the original.Code:char *pStudent = new char[sizeof(studentName)+1]; strcpy(pStudent, studentName.c_str()); char *pOStudent = new char[sizeof(obj.studentName)]; strcpy(pOStudent, obj.studentName); strupr(pStudent); strupr(pOStudent); ... ... ... delete [] pStudent; delete [] pOStudent;
Almost. If the second new[] throws std::bad_alloc then you would get a memory leak since you would not be able to use delete[] on the pointer returned by the first new[].Originally Posted by Phyxashun
Why are you so resistant to using std::string and my suggestion?
EDIT:
Wait a minute. studentName is a std::string, so that use of sizeof is wrong. You would use studentName.size() (or studentName.length()) instead.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
And why doesn't this logic hold?
There is no reason why haven't tried std::string yet. From what I can tell that will be the route I take. Right now I am just fiddling around with the function, to see what I can come up with.Code:if(pStudent == pOStudent) ...
Thanks!
As in it does not actually do a case insensitive comparison? That would be because pStudent and pOStudent are pointers, so you are merely comparing addresses. You would need to use strcmp() if you really want to use C-style strings.Originally Posted by Phyxashun
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)