C++ 蓝桥杯 基础练习 字符串对比

版权声明:作者:Valarjin https://blog.csdn.net/weixin_42125215/article/details/88617846

C++基础练习 字符串对比

问题描述:
  给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一:
  1:两个字符串长度不等。比如 Beijing 和 Hebei
  2:两个字符串不仅长度相等,而且相应位置上的字符完全一致(区分大小写),比如 Beijing 和 Beijing
  3:两个字符串长度相等,相应位置上的字符仅在不区分大小写的前提下才能达到完全一致(也就是说,它并不满足情况2)。比如 beijing 和 BEIjing
  4:两个字符串长度相等,但是即使是不区分大小写也不能使这两个字符串一致。比如 Beijing 和 Nanjing
  编程判断输入的两个字符串之间的关系属于这四类中的哪一类,给出所属的类的编号。

输入格式:
  包括两行,每行都是一个字符串
  
输出格式:
  仅有一个数字,表明这两个字符串的关系编号

样例输入:
BEIjing
beiJing

样例输出:
3

#include<iostream>
#include<algorithm>	//transform
#include<cctype>
#include<string>	//toupper,tolower
using namespace std;
int main() 
{
	string str1, str2;
	cin >> str1;
	cin >> str2;
	if (str1.size() == str2.size()) 
	{
		if (str1 != str2) 
		{
			transform(str1.begin(), str1.end(), str1.begin(),::toupper);	//转换大小写
			transform(str2.begin(), str2.end(), str2.begin(), ::toupper);
			if (str1 == str2) 
			{
				cout << "3" << endl;
			}
			else 
			{
				cout << "4" << endl;
			}
		}
		else 
		{
			cout << "2" << endl;
		}
	}
	else 
	{
		cout << "1" << endl;
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42125215/article/details/88617846
今日推荐