计蒜客: T1114 忽略大小写的字符串比较

题目链接:忽略大小写的字符串比较

解题思路:先统一标准,将两个字符串全都转换为小写。然后再进行比较。注意:题目并没有说按照长度来区分字符串大小,一定不要按照长度来区分字符串大小。

#include<iostream>
#include<string>
using namespace std;

int main(){
	string str1,str2;
	int len1,len2; 
	getline(cin,str1);
	getline(cin,str2);
	
	len1 = str1.size();
	len2 = str2.length();
	
	//统一标准,全都转换为小写 
	for(int i=0;i<len1;i++){
		if(str1[i]>='A' && str1[i]<='Z'){
			str1[i] += 32;
		}
	}
	for(int i=0;i<len2;i++){
		if(str2[i]>='A' && str2[i]<='Z'){
			str2[i] += 32;
		}
	}
	
	int com;
	
	com = str1.compare(str2);
	
	//在compare()函数中,如果两个字符串相同就返回0、大于返回正数、小于返回负数 
	if(com == 0){
		cout<<"=";
	}else if(com > 0){
		cout<<">";
	}else if(com < 0){
		cout<<"<"; 
	}
	
	return 0;
	
} 
发布了61 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41575507/article/details/104487923