C++에서 대소 문자를 무시하는 두 문자열 비교

Jinku Hu 2023년10월12일
  1. strcasecmp 함수를 사용하여 대소 문자를 무시하는 두 문자열 비교
  2. strncasecmp 함수를 사용하여 대소 문자를 무시하는 두 문자열 비교
  3. 사용자 지정toLower 함수 및==연산자를 사용하여 대소 문자를 무시하는 두 문자열 비교
C++에서 대소 문자를 무시하는 두 문자열 비교

이 기사에서는 C++에서 대소 문자를 무시하면서 두 문자열을 비교하는 방법에 대한 여러 방법을 보여줍니다.

strcasecmp 함수를 사용하여 대소 문자를 무시하는 두 문자열 비교

strcasecmp<cstring>헤더를 사용하여 C++ 소스 파일에 포함될 수있는 C 표준 라이브러리 함수입니다. 함수 자체는 바이트 단위로 작동하며 해당 문자열이 평가 될 때 0보다 작거나 같거나 큰 정수를 반환합니다.

즉, 무시 된 두 문자열이 대소 문자가 같으면 반환 값은 0입니다. 다른 시나리오에서는 첫 번째 다른 문자가 발견되면 알파벳 위치에 대한 값과 비교되고 해당 결과가 반환됩니다.

#include <cstring>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
  string text1 = "Hey! Mr. Tambourine man, play a song for me";
  string text2 = "hey! Mr. Tambourine man, PLAY a song for me";
  string text3 = "Hey! Mrs. Tambourine man, play a song for me";

  if (strcasecmp(text1.c_str(), text2.c_str()) == 0) {
    cout << "strings: text1 and text2 match." << endl;
  } else {
    cout << "strings: text1 and text2 don't match!" << endl;
  }

  if (strcasecmp(text1.c_str(), text3.c_str()) == 0) {
    cout << "strings: text1 and text3 match." << endl;
  } else {
    cout << "strings: text1 and text3 don't match!" << endl;
  }

  return EXIT_SUCCESS;
}

출력:

strings: text1 and text2 match.
strings: text1 and text3 don't match!

strncasecmp 함수를 사용하여 대소 문자를 무시하는 두 문자열 비교

strncasecmp는 대소 문자를 무시하여 두 문자열에서 주어진 수의 문자를 비교하는 데 사용할 수있는 위 함수의 또 다른 변형입니다. 이 함수는 첫 번째 char에서 비교해야하는 최대 문자 수에 대한 정수 값을 사용합니다. 다음 예제는 대소 문자를 무시하고 두 문자열의 처음 5자를 비교하는 방법을 보여줍니다.

#include <cstring>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

constexpr int BYTES_TO_COMPARE = 5;

int main() {
  string text1 = "Hey! Mr. Tambourine man, play a song for me";
  string text3 = "hey! Mrs. Tambourine man, PLAY a song for me";

  if (strncasecmp(text1.c_str(), text3.c_str(), BYTES_TO_COMPARE) == 0) {
    printf("The first %d characters of strings: text1 and text3 match.\n",
           BYTES_TO_COMPARE);
  }

  return EXIT_SUCCESS;
}

출력:

The first 5 characters of strings: text1 and text3 match.

사용자 지정toLower 함수 및==연산자를 사용하여 대소 문자를 무시하는 두 문자열 비교

또는 문자열을 동일한 대소 문자로 변환 한 다음 비교를 위해 간단한==연산자를 사용할 수 있습니다. 임의의 선택으로,이 예제는string 객체를 반환하는 사용자 정의 함수를 사용하여 두 문자열을 모두 낮 춥니 다. 마지막으로if 문은 비교 표현식을 포함 할 수 있습니다.

#include <cstring>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

std::string toLower(std::string s) {
  std::transform(s.begin(), s.end(), s.begin(),
                 [](unsigned char c) { return std::tolower(c); });
  return s;
}

int main() {
  string text1 = "Hey! Mr. Tambourine man, play a song for me";
  string text2 = "Hey! Mr. Tambourine man, play a song for me";
  string text3 = "Hey! Mrs. Tambourine man, play a song for me";

  if (toLower(text1) == toLower(text2)) {
    cout << "strings: text1 and text2 match." << endl;
  } else {
    cout << "strings: text1 and text2 don't match!" << endl;
  }

  if (toLower(text1) == toLower(text3)) {
    cout << "strings: text1 and text3 match." << endl;
  } else {
    cout << "strings: text1 and text3 don't match!" << endl;
  }

  return EXIT_SUCCESS;
}

출력:

strings: text1 and text2 match.
strings: text1 and text3 don't match!
작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - C++ String