水题不解释,考研复习压力偶尔写一道换换心情还不错~
这里有一个比较有趣的知识点,对于同时输入多个字符串时还要允许空格的输入,那么普通的cin函数就不能满足要求了,这里采用getline函数解决,如下:
string s1,s2;getline(cin,s1);getline(cin,s2);
剔除字符串中的空格
string DeBlack(string s)
{string n;for(int i=0;i<=s.size()-1;i++)if(s[i]!=' ')n+=s[i];return n;
}
统一字符为大写
string ConDX(string s)
{for(int i=0;i<=s.size()-1;i++)if(s[i]>=97&&s[i]<=122)s[i]=s[i]-32;return s;
}
判断字符串是否相等
int DoQua(string s1,string s2)
{if(s1.size()!=s2.size())return -1;for(int i=0;i<=s1.size()-1;i++){if(s1[i]!=s2[i])return -1;}return 1;
}
如下是完整代码,使用前调试的结果正确
#include <iostream>
#include <string>#include <vector>
using namespace std;string DeBlack(string s)
{string n;for(int i=0;i<=s.size()-1;i++)if(s[i]!=' ')n+=s[i];return n;
}string ConDX(string s)
{for(int i=0;i<=s.size()-1;i++)if(s[i]>=97&&s[i]<=122)s[i]=s[i]-32;return s;
}
int DoQua(string s1,string s2)
{if(s1.size()!=s2.size())return -1;for(int i=0;i<=s1.size()-1;i++){if(s1[i]!=s2[i])return -1;}return 1;
}int main(int argc, char** argv)
{string s1,s2;getline(cin,s1);getline(cin,s2);string n1=DeBlack(s1);n1=ConDX(n1);string n2=DeBlack(s2);n2=ConDX(n2);// cout<<n1<<" & "<<n2<<endl;int ans=DoQua(n1,n2);if(ans==1)cout<<"YES"<<endl;elsecout<<"NO"<<endl;return 0;
}