重複の可能性:
C++での大文字と小文字を区別しない文字列の比較
2つの文字列が等しいかどうかを比較するために、C++用のコードをいくつか作成しました。私が欲しいのは校正です。将来的にはもっと多くのプログラムでこれを使うことを計画しているので、この関数がうまく機能することが重要です。この機能は、再利用可能、ポータブルなどの機能のように見えますか?これを行うためのより「最新の」方法はありますか?私はacライブラリを使用しましたが、これはc ++プログラムです、それはタブーですか?
ありがとう、JH。
//function to compare two strings regardless of case
//-- returns true if the two strings are equal
//-- returns false if
// --the strings are unequal
// --one of the strings is longer than 255 chars
bool isEqual(string str1, string str2){
if(str1.length()!=str2.length()) //the strings are different lengths,
return false; //they can't be equal
if((str1.length()>255) || (str2.length()>255))
return false;
char * cstr1 = new char [str1.length()+1];
strcpy (cstr1, str1.c_str());
char * cstr2 = new char [str2.length()+1];
strcpy (cstr2, str2.c_str());
for(int i=0; i<str1.length()+1; i++){
if(toupper(cstr1[i]) != toupper(cstr2[i]))
return false;
}
return true;
}