描述
打印机的大小写错乱了,并且空格也不受控制了,原原园长希望你写一个程序判断她写的两句话是不是一样的。
判断两个由大小写字母和空格组成的字符串是否相等,要求忽略大小写,且忽略空格。
输入描述
两行,每行包含一个字符串。(长度在1000以内)
输出描述
若两个字符串相等,输出YES,否则输出NO。
代码
1.使用STL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <bits/stdc++.h> using namespace std; void x(string &str){ str.erase(remove(str.begin(), str.end(), ' '), str.end()); } int main(){ string a,b; getline(cin,a); getline(cin,b); x(a); x(b); if(a.size()!=b.size()){ cout << "NO1"; return 0; } for(int i = 0;i<a.size();i++){ if(tolower(a[i]) != tolower(b[i])){ cout << "NO"; return 0; } } cout << "YES"; return 0; }
|
2.比较时忽略空格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include<bits/stdc++.h> using namespace std; int main(){ string a, b; int i; getline(cin, a); getline(cin, b); for(int i = 0; i < a.length(); i++){ if(isupper(a[i])){ a[i] += 32; } } for(int i = 0; i < b.length(); i++){ if(isupper(b[i])){ b[i] += 32; } } string x,y; for(i = 0; i < a.length(); i++){ if(a[i] != ' '){ x.push_back(a[i]); } } for(i = 0; i < b.length(); i++){ if(b[i] != ' '){ y.push_back(b[i]); } } if(x == y){ cout << "YES"; } else{ cout << "NO"; } return 0; }
|