計蒜客: 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;
	
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章