logo

Rôzne spôsoby porovnávania reťazcov v C++

Táto časť sa bude zaoberať rôznymi spôsobmi porovnávania daných reťazcov v programovacom jazyku C++. Porovnanie reťazca určuje, či sa prvý reťazec rovná inému reťazcu alebo nie. Príklad: HELLO a Hello sú dva rôzne reťazce.

Rôzne spôsoby porovnávania reťazcov v C++

Existujú rôzne spôsoby, ako porovnať reťazce v programovacom jazyku C++, a to takto:

  1. Pomocou funkcie strcmp().
  2. Použitie funkcie porovnanie ().
  3. Použitie relačného operátora
  4. Použitie cyklu For a príkazu If
  5. Používanie funkcie definovanej používateľom

funkcia strcmp().

Strcmp() je preddefinovaná knižničná funkcia reťazec.h hlavičkový súbor. Funkcia strcmp() porovnáva dva reťazce na lexikografickom základe. To znamená, že funkcia strcmp() začne porovnávať prvý reťazec s druhým reťazcom, znak po znaku, kým nie sú všetky znaky v oboch reťazcoch rovnaké alebo kým nenájdete znak NULL.

fcfs

Syntax

 int strcmp ( const char *leftstr, const char *rightstr ); 

Parametre:

leftstr: Definuje znaky ľavého reťazca.

rightstr: Definuje znaky správneho reťazca.

Vrátenie:

java cast int do reťazca

Reťazec leftstr porovnáva každý znak s druhým reťazcom z ľavej strany až po koniec oboch reťazcov. A ak sú oba reťazce rovnaké, funkcia strcmp() vráti reťazce rovnaké. V opačnom prípade reťazce nie sú rovnaké.

Vytvorme program na porovnávanie reťazcov pomocou funkcie strcmp() v C++.

Program1.cpp

 #include using namespace std; #include int main () { // declare strings const char *str1 = ' Welcome to JavaTpoint'; const char *str2 = ' Welcome to JavaTpoint'; const char *str3 = ' JavaTpoint'; const char *str4 = ' Javatpoint'; cout << ' String 1: ' << str1 << endl; cout << ' String 2: ' << str2 << endl; // use strcmp() function to validate the strings are equal if (strcmp (str1, str2) == 0) { cout << ' 
 Both strings are equal. ' << endl; } else { cout << ' The strings are not equal. ' << endl; } cout << ' 
 String 3: ' << str3 << endl; cout << ' String 4: ' << str4 << endl; // use strcmp() function to validate the strings are equal if (strcmp (str3, str4) == 0) { cout << ' 
 Both strings are equal. ' << endl; } else cout << ' 
 The strings are not equal. '; return 0; } 

Výkon

 String 1: Welcome to JavaTpoint String 2: Welcome to JavaTpoint Both strings are equal. String 3: JavaTpoint String 4: Javatpoint The strings are not equal. 

Funkcia porovnať().

Funkcia Compare() je preddefinovaná knižničná funkcia jazyka C++. Funkcia Compare() porovnáva dva dané reťazce a vracia nasledujúce výsledky na základe prípadov zhody:

  1. Ak sú oba reťazce rovnaké, funkcia vráti 0.
  2. Ak je hodnota znaku prvého reťazca menšia ako druhého reťazca, funkcia sa vráti<0.< li>
  3. Ak je druhý reťazec väčší ako prvý, funkcia vráti hodnotu väčšiu ako 0 alebo >0.

Syntax

 int compare (const string &amp;str) const; 

Vytvorme jednoduchý program na porovnanie dvoch reťazcov pomocou funkcie Compare() v C++.

Program2.cpp

hlavný program v jave
 #include using namespace std; int main () { string str1, str2; // declare string variable cout &lt;&gt; str1; cout &lt;&gt; str2; // use compare() function to compare the second string with first string int i = str1.compare(str2); if ( i <0) { cout << str1 ' is smaller than str2 string' <<endl; } else if ( i> 0) { cout &lt;&lt; str2 &lt;&lt; &apos; is greater than &apos; &lt;&lt; str1 &lt;&lt; &apos; string.&apos; &lt;&lt; endl; } else // i == 0; { cout &lt;&lt; &apos; Both strings are equal.&apos;; } return 0; } </0)>

Výkon

 1st Run: Enter the string 1: Program Enter the string 2: program Program is smaller than program string 2nd Run: Enter the string 1: APPLE Enter the string 2: APPLE Both strings are equal. 

Relačný operátor

Je to operátor používaný na porovnanie dvoch reťazcov alebo číselných hodnôt v C++. C++ má rôzne typy relačných operátorov, ako napríklad '==', '!=', >,

Program3.cpp

 #include using namespace std; int main () { // declare string variables string str1; string str2; cout &lt;&lt; &apos; Enter the String 1: &apos; &lt;&gt; str1; cout &lt;&lt; &apos; Enter the String 2: &apos; &lt;&gt; str2; // use &apos;==&apos; equal to operator to check the equality of the string if ( str1 == str2) { cout &lt;&lt; &apos; String is equal.&apos; &lt;&lt; endl; } else { cout &lt;&lt; &apos; String is not equal.&apos; &lt;&lt; endl; } return 0; } 

Výkon

 Enter the String 1: JavaTpoint Enter the String 2: javatpoint String is not equal. 

2ndprevedenie:

 Enter the String 1: Program Enter the String 2: Program String is equal. 

Porovnajte dva reťazce pomocou relačného operátora Nerovná sa (!=).

Vytvorme program na porovnanie, či sú reťazce rovnaké alebo nie, pomocou operátora Nerovná sa (!=) v C++.

Program4.cpp

 #include using namespace std; int main () { // declare string variables string str1; string str2; cout &lt;&lt; &apos; Enter the String 1: &apos; &lt;&gt; str1; cout &lt;&lt; &apos; Enter the String 2: &apos; &lt;&gt; str2; // use &apos;!=&apos; not equal to operator to check the equality of the string if ( str1 != str2) { cout &lt;&lt; &apos; String is not equal.&apos; &lt;&lt; endl; } else { cout &lt;&lt; &apos; String is equal.&apos; &lt;&lt; endl; } return 0; } 

Výkon

 Enter the String 1: JAVATpoint Enter the String 2: JavaTPOINT String is not equal. 

2ndSpustiť:

mb vs gb
 Enter the String 1: HELLO Enter the String 2: HELLO String is equal. 

Porovnajte dva reťazce pomocou príkazu for a if v C++

Program5.cpp

 #include using namespace std; int main () { char s1[50], s2[50]; // declare character array int i, disp; cout &lt;&lt; &apos; Enter the String 1: &apos; &lt;&gt; s1; cout &lt;&lt; &apos; Enter the String 2: &apos; &lt;&gt; s2; for (i = 0; s1[i] == s2[i] &amp;&amp; s1[i] == &apos;&apos;; i++); if (s1[i] <s2[i]) 1 2 { cout < s2[i]) << ' string is less than 1'; } else equal to 2'; return 0; pre> <p> <strong>Output</strong> </p> <pre> Enter the String 1: WELCOME Enter the String 2: WELCOME String 1 is equal to String 2 </pre> <h3>Compare two strings using the User-defined function in C++</h3> <p>Let&apos;s create a simple program to compare the first string with another string using the user-defined function in C++.</p> <p> <strong>Program6.cpp</strong> </p> <pre> #include using namespace std; void RelationalCompare ( string str1, string str2) { // use relational not equal operator if ( str1 != str2) { cout &lt;&lt; str1 &lt;&lt; &apos; is not equal to &apos; &lt;&lt; str2 &lt;&lt; &apos; string. &apos; &lt; str2) { cout &lt;&lt; str1 &lt;&lt; &apos; is greater than &apos; &lt;&lt; str2 &lt;&lt; &apos; string.&apos; &lt;&lt; endl; } else { cout &lt;&lt; str2 &lt;&lt; &apos; is greater than &apos; &lt;&lt; str1 &lt;&lt; &apos; string.&apos; &lt;&lt; endl; } } else cout &lt;&lt; str1 &lt;&lt; &apos; is equal to &apos; &lt;&lt; str2 &lt;&lt; &apos; string.&apos; &lt;&lt; endl; } int main () { string str1 ( &apos;JavaT&apos;); string str2 ( &apos;Tpoint&apos;); // call function RelationalCompare (str1, str2); string str3 (&apos;JavaTpoint&apos;); string str4 (&apos;JavaTpoint&apos;); RelationalCompare (str3, str4); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> JavaT is not equal to Tpoint string. Tpoint is greater than JavaT string. JavaTpoint is equal to JavaTpoint string. </pre> <hr></s2[i])>

Porovnajte dva reťazce pomocou funkcie definovanej používateľom v C++

Vytvorme jednoduchý program na porovnanie prvého reťazca s iným reťazcom pomocou užívateľom definovanej funkcie v C++.

Program6.cpp

 #include using namespace std; void RelationalCompare ( string str1, string str2) { // use relational not equal operator if ( str1 != str2) { cout &lt;&lt; str1 &lt;&lt; &apos; is not equal to &apos; &lt;&lt; str2 &lt;&lt; &apos; string. &apos; &lt; str2) { cout &lt;&lt; str1 &lt;&lt; &apos; is greater than &apos; &lt;&lt; str2 &lt;&lt; &apos; string.&apos; &lt;&lt; endl; } else { cout &lt;&lt; str2 &lt;&lt; &apos; is greater than &apos; &lt;&lt; str1 &lt;&lt; &apos; string.&apos; &lt;&lt; endl; } } else cout &lt;&lt; str1 &lt;&lt; &apos; is equal to &apos; &lt;&lt; str2 &lt;&lt; &apos; string.&apos; &lt;&lt; endl; } int main () { string str1 ( &apos;JavaT&apos;); string str2 ( &apos;Tpoint&apos;); // call function RelationalCompare (str1, str2); string str3 (&apos;JavaTpoint&apos;); string str4 (&apos;JavaTpoint&apos;); RelationalCompare (str3, str4); return 0; } 

Výkon

 JavaT is not equal to Tpoint string. Tpoint is greater than JavaT string. JavaTpoint is equal to JavaTpoint string.