C++에서 문자열을 비교하는 3가지 방법
소개
이 기사에서는 C++에서 문자열을 비교하는 방법을 배웁니다.
C++의 문자열은 다음 기술 중 하나를 사용하여 비교할 수 있습니다.
String
strcmp()
함수- 내장
compare()
함수 - C++ 관계 연산자(
==
,!=
)
1. C++에서 String strcmp() 함수 사용하기
C++ String
에는 String
유형의 데이터를 조작하기 위한 내장 함수가 있습니다. strcmp()
함수는 사전적 방식으로 두 문자열을 비교하는 데 사용되는 C 라이브러리 함수입니다.
strcmp() 구문
- 입력 문자열은 C 스타일 문자열의
char
배열이어야 합니다. strcmp()
는 대소문자 구분 형식의 문자열도 비교합니다.
int strcmp(const char *str1, const char *str2);
이 함수는 일치하는 경우에 따라 다음 값을 반환합니다.
- 두 문자열이 같으면
0
을 반환합니다. - 첫 번째 문자열의 문자 값이 두 번째 문자열 입력에 비해 작은 경우
< 0
(0 미만)을 반환합니다. - 두 번째 문자열이 비교할 때 더 큰 경우 결과는
> 0
(0보다 큼)이 됩니다.
strcmp() 예제 1
다음 코드를 실행합니다.
#include <iostream>
#include <string.h>
int main()
{
const char *str_inp1 = "String Match";
const char *str_inp2 = "String Unmatch";
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (strcmp(str_inp1, str_inp2) == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else
std::cout << "\nThe input strings are not equal." << std::endl;
}
그러면 다음과 같은 출력이 생성됩니다.
OutputString 1: String Match
String 2: String Unmatch
The input strings are not equal.
strcmp(str_inp1, str_inp2)
결과는 -9
입니다. str_inp1
과 str_inp2
의 값이 다릅니다.
strcmp() 예제 2
다음 코드를 실행합니다.
#include <iostream>
#include <string.h>
int main()
{
const char *str_inp1 = "String Match";
const char *str_inp2 = "String Match";
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (strcmp(str_inp1, str_inp2) == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else
std::cout << "\nThe input strings are not equal." << std::endl;
}
그러면 다음과 같은 출력이 생성됩니다.
OutputString 1: String Match
String 2: String Match
Both the input strings are equal.
strcmp(str_inp1, str_inp2)
결과는 0
입니다. str_inp1
과 str_inp2
의 값은 동일합니다.
2. C++에서 compare() 함수 사용
C++에는 두 문자열을 비교하는 내장 compare()
함수가 있습니다.
비교() 구문
compare()
함수는 두 문자열을 비교합니다.
int compare (const string& string-name) const;
이 함수는 일치하는 경우에 따라 다음 값을 반환합니다.
- 두 문자열이 같으면
0
을 반환합니다. - 첫 번째 문자열의 문자 값이 두 번째 문자열 입력에 비해 작은 경우
< 0
(0 미만)을 반환합니다. - 두 번째 문자열이 비교할 때 더 큰 경우 결과는
> 0
(0보다 큼)이 됩니다.
예제 1: compare() 사용
다음 코드를 실행합니다.
#include <iostream>
int main()
{
std::string str_inp1("String Match");
std::string str_inp2("String Match");
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
int res = str_inp1.compare(str_inp2);
if (res == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else if (res < 0)
std::cout << "\nString 1 is smaller as compared to String 2." << std::endl;
else
std::cout << "\nString 1 is greater as compared to String 2." << std::endl;
}
이 예에서 str_inp1
및 str_inp2
는 compare()
와 비교됩니다.
OutputString 1: String Match
String 2: String Match
Both the input strings are equal.
두 문자열은 사전식으로 동일하므로 함수는 0
을 반환합니다.
예제 2: compare() 사용
다음 코드를 실행합니다.
#include <iostream>
int main()
{
std::string str_inp0("String Match");
std::string str_inp1("String Match");
std::string str_inp2("String Unmatch");
std::cout << "String 1: " << str_inp1 << std::endl;
if (str_inp1.compare(str_inp0) == 0)
std::cout << "\nStrings are equal." << std::endl;
else
std::cout << "\nStrings are not equal." << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (str_inp2.compare(str_inp0) == 0)
std::cout << "\nStrings are equal." << std::endl;
else
std::cout << "\nStrings are not equal." << std::endl;
}
이 예에서 str_inp0
은 str_inp1
과 비교됩니다.
OutputString 1: String Match
Strings are equal.
그런 다음 str_inp0
을 str_inp2
와 비교합니다.
OutputString 2: String Unmatch
Strings are not equal.
이 코드는 문자열을 compare()
함수에 대한 다른 입력 문자열과 직접 비교했습니다.
3. C++의 관계 연산자
==
(이중 같음) 및 !=
(같지 않음)와 같은 C++ 관계 연산자는 문자열 비교에 유용할 수 있습니다.
관계 연산자 구문
두 값이 같은지 확인합니다.
string1 == string2
두 값이 같지 않은지 확인합니다.
string1 != string2
예제 1: C++ == 연산자 사용
다음 코드를 실행합니다.
#include <iostream>
int main()
{
std::string str_inp1;
std::string str_inp2;
std::cout << "Enter the String 1:\n";
std::cin >> str_inp1;
std::cout << "Enter the String 2:\n";
std::cin >> str_inp2;
if (str_inp1 == str_inp2)
std::cout << "Strings are equal" << std::endl;
else
std::cout << "Strings are not equal" << std::endl;
}
"문자열 1\ 및 "문자열 2\에 대한 값을 제공하십시오.
Enter the String 1:
DigitalOcean
Enter the String 2:
digitalocean
Strings are not equal
코드는 ==
로 두 문자열을 비교합니다.
예제 2: C++ != 연산자 사용
다음 코드를 실행합니다.
#include <iostream>
int main()
{
std::string str_inp1;
std::string str_inp2;
std::cout << "Enter the String 1:\n";
std::cin >> str_inp1;
std::cout << "Enter the String 2:\n";
std::cin >> str_inp2;
if (str_inp1 != str_inp2)
std::cout << "Strings are not equal" << std::endl;
else
std::cout << "Strings are equal" << std::endl;
}
"문자열 1\ 및 "문자열 2\에 대한 값을 제공하십시오.
Enter the String 1:
DigitalOcean
Enter the String 2:
DigitalOcean
Strings are equal
코드는 두 문자열을 !=
로 비교합니다.
결론
이 기사에서는 C++에서 문자열을 비교하는 방법을 배웠습니다. 여기에는 String
의 strcmp()
함수, 내장 compare()
함수 및 관계 연산자(==
, <코드>!=).
더 많은 C++ 자습서로 학습을 계속하십시오.