just like the title i would like to create a program in C where when you compare strings it is non-case sensitive...
How do you make a program in C where the strings are non-case sensitive?
cstrings (char*) will always be case sensitive because you are just storing an array of character length data.
You can however do a case insentive comparison by calling stricmp instead of strcmp.
If your using linux the function is strcasecmp() .
Remember if you want avoid memory leaks use the strn version (strnicmp) and specify a length.
Reply:Too much edumacational **** fo me.
Reply:Create two temp strings: str1 and str2, convert both strings into lower/upper case and compare (strcamp) them will do.
Reply:Well, to be honest, I am not a C developer, but I can tell you that in other languages, such as C#, Java, and VB, I was taught to convert the strings to be compared to all upper or all lower cases before comparing them. I am not sure if there is a similar conversion type function built into C or not, but if there is, that is how I would do it. Good luck.
Reply:You can use the standard C functions stricmp() and strnicmp(), or you could convert CStrings to a uniform case using the MakeUpper() or MakeLower() member functions. But this can be a problem if the original case of the CString objects needs preserving.
A couple of examples:
1. If you were searching for strings:
BOOL CMyClass::FindNoCase(CString sS1, CString sS2)
{
// Search a string for another string
// irrespective of case.
CString sToBeSearched = sS1;
sToBeSearched.MakeLower();
CString sFindThis = sS2;
sFindThis.MakeLower();
if(sToBeSearched.Find(sFindThis) != -1)
return TRUE;
else
return FALSE;
}
2. If you just wanted to do a direct comparison:
BOOL CMyClass::EqualsNoCase(CString sS1, CString sS2)
{
// compare strings
// irrespective of case
CString sFirst = sS1;
sFirst.MakeLower();
CString sSecond = sS2;
sSecond.MakeLower();
if(sFirst == sSecond)
return TRUE;
else
return FALSE;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment