我是初學者。我正在嘗試創建一個按字母順序比較兩個字串的程式。但它會忽略文本大小寫。我正面臨著問題。如何忽略C 中的文本大小寫?
#include <iostream>
using namespace std;
int main() {
string a, b;
cin >> a;
cin >> b;
if ( a > b) {
cout << "1";
}
else if ( a < b) {
cout << "-1";
}
else if (a == b) {
cout << "0";
}
}
uj5u.com熱心網友回復:
在比較之前,您可以通過以下方式將兩個字串都轉換為小寫std::tolower:
for (auto& c : a) c = std::tolower(static_cast<unsigned char>(c));
for (auto& c : b) c = std::tolower(static_cast<unsigned char>(c));
uj5u.com熱心網友回復:
使用不區分大小寫的比較函式,例如 C's或帶有標志的strcmpi()Windows's或 Posix's等CompareStringA()NORM_IGNORECASEstrcasecmp()
uj5u.com熱心網友回復:
我建議使用回圈并使用std::toupper或將兩個字串轉換為小寫或小寫std::tolower
for(const auto& i:a)x=std::tolower(x);
for(const auto& i:a)x=std::tolower(x);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/468115.html
下一篇:在SpringAuthorizationServer(0.2.3 )中支持并發全堆疊MVC(session)認證以及無狀態JWT認證
