AcWing 768. 忽略大小写比较字符串大小---(语法基础班(第五章字符串)--简单)
原题链接
简单
作者:
初静
,
2021-02-17 11:04:02
,
所有人可见
,
阅读 308
三种写法:
#include <cstring>
using namespace std;
int main()
{
string a,b;
getline(cin,a);
getline(cin,b);
for (int i = 0; i < a.size(); i ++)
if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 32;
for (int i = 0; i < b.size(); i ++)
if (b[i] >= 'A' && b[i] <= 'Z') b[i] += 32;
int c;
c = strcmp(a.c_str(),b.c_str()); //!!!!
if(c < 0) cout << "<";
if(c == 0) cout << "=";
if(c > 0) cout << ">";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string a,b;
getline(cin,a);
getline(cin,b);
for(auto &c:a) c = tolower(c); //!!!
for(auto &c:b) c = tolower(c);
if(a == b) cout << '='; //!!!
if(a > b) cout << '>';
if(a < b) cout << '<';
}
#include <iostream>
using namespace std;
int main()
{
string a, b;
getline(cin, a);
getline(cin, b);
int t = 0;
// int m = a.size()>b.size() ? a.size():b.size();
int m = max(a.size(), b.size()); //!!!!
for(int i = 0; i < m; i++)
{
if(a[i] >= 'a' && a[i] <= 'z') a[i] -= 32; //!!!!
if(b[i] >= 'a' && b[i] <= 'z') b[i] -= 32;
if(a[i] == b[i])
t++;
else if(a[i] > b[i])
{
cout << ">" << endl;
break;
}
else if(a[i] < b[i])
{
cout << "<" << endl;
break;
}
}
if(t == m) cout << "=" << endl; // !!!!!
return 0;
}