C++에서는 표준 라이브러리에 문자열이 있습니다. 이 프로그램에서는 두 문자열이 동일한지 여부를 확인하는 방법을 볼 것입니다. 이 경우 대소문자를 무시합니다.
여기서 논리는 간단합니다. 전체 문자열을 소문자 또는 대문자 문자열로 변환한 다음 비교하여 결과를 반환합니다.
문자열을 소문자 문자열로 변환하는 변환 함수를 얻기 위해 알고리즘 라이브러리를 사용했습니다.
Input: Two strings “Hello WORLD” and “heLLO worLD” Output: Strings are same
알고리즘
Step 1: Take two strings str1, and str2 Step 2: Convert str1, and str2 into lowercase form Step 3: Compare str1 and str2 Step 4: End
예시 코드
#include<iostream>
#include <algorithm>
using namespace std;
int case_insensitive_match(string s1, string s2) {
//convert s1 and s2 into lower case strings
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
if(s1.compare(s2) == 0)
return 1; //The strings are same
return 0; //not matched
}
main() {
string s1, s2;
s1 = "Hello WORLD";
s2 = "heLLO worLD";
if(case_insensitive_match(s1, s2)) {
cout << "Strings are same";
}else{
cout << "Strings are not same";
}
} 출력
Strings are same