C++で文字列を比較する3つの方法

0 株式
0
0
0
0

導入

この記事では、C++ で文字列を比較する方法を学習します。.

1. C++でstrcmp()関数を使用する

C++ Stringには、文字列データを操作するための組み込み関数があります。strcmp()関数は、2つの文字列を辞書順に比較するために使用されるCライブラリ関数です。.

  • 入力文字列は、C スタイルの文字列の文字配列である必要があります。.
  • strcmp() は大文字と小文字を区別して文字列を比較します。.
int strcmp(const char *str1, const char *str2);

この関数は、一致する項目に応じて次の値を返します。

  • 両方の文字列が同じ場合は 0 を返します。.
  • 最初の文字列の文字値が 2 番目の文字列入力よりも小さい場合は、< 0 (ゼロ未満) を返します。.
  • 比較の 2 番目の文字列が大きい場合、結果は > 0 (ゼロより大きい) になります。.

次のコードを実行します。

#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 の値が異なります。.

次のコードを実行します。

#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;
}

これにより、次の出力が生成されます。

Output
String 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++ には、2 つの文字列を比較するための compare() 関数が組み込まれています。.

compare() 関数は 2 つの文字列を比較します。

int compare (const string& string-name) const;

この関数は、一致する項目に応じて次の値を返します。

  • 両方の文字列が同じ場合は 0 を返します。.
  • 最初の文字列の文字値が 2 番目の文字列入力よりも小さい場合は、< 0 (ゼロ未満) を返します。.
  • 比較の 2 番目の文字列が大きい場合、結果は > 0 (ゼロより大きい) になります。.

次のコードを実行します。

#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() で比較されます。

Output
String 1: String Match
String 2: String Match
Both the input strings are equal.

両方の文字列は語彙的に同一なので、関数は 0 を返します。.

次のコードを実行します。

#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 と比較されます。

Output
String 1: String Match
Strings are equal.

次に、str_inp0 と str_inp2 を比較します。

Output
String 2: String Unmatch
Strings are not equal.

このコードは、compare() 関数を使用して文字列を別の入力文字列と直接比較します。.

3. C++における関係演算子

== (等しい) や != (等しくない) などの C++ 関係演算子は、文字列の比較に役立ちます。.

2つの値が等しいかどうかを確認します。

string1 == string2

2つの値が等しくないかどうかを確認します。

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 つの文字列を == で比較します。.

例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

このコードは != を使用して 2 つの文字列を比較します。.

結果

この記事では、C++における文字列比較メソッドについて学びました。String関数のstrcmp()、組み込み関数のcompare()、そして関係演算子(==、!=)について学びました。.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

あなたも気に入るかもしれない