logo

Različiti načini usporedbe nizova u C++

U ovom odjeljku raspravljat će se o različitim načinima usporedbe zadanih nizova u programskom jeziku C++. Usporedba niza određuje je li prvi niz jednak drugom nizu ili ne. Primjer: HELLO i Hello dva su različita niza.

Različiti načini usporedbe nizova u C++

Postoje različiti načini za usporedbu nizova u programskom jeziku C++, kako slijedi:

  1. Korištenje funkcije strcmp().
  2. Korištenje funkcije compare().
  3. Korištenje relacijskog operatora
  4. Korištenje For petlje i If naredbe
  5. Korištenje korisnički definirane funkcije

funkcija strcmp().

Strcmp() je unaprijed definirana funkcija biblioteke niz.h datoteka zaglavlja. Funkcija strcmp() uspoređuje dva niza na leksikografskoj osnovi. To znači da funkcija strcmp() počinje uspoređivati ​​prvi niz s drugim nizom, znak po znak sve dok svi znakovi u oba niza ne budu isti ili se ne naiđe na NULL znak.

Sintaksa

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

Parametri:

lijevo str: Definira znakove lijevog niza.

desni str: Definira znakove desnog niza.

Povratak:

Niz leftstr uspoređuje svaki znak s drugim nizom s lijeve strane do kraja oba niza. A ako su oba niza jednaka, funkcija strcmp() vraća nizove koji su jednaki. Inače, žice nisu jednake.

Kreirajmo program za usporedbu nizova pomoću funkcije strcmp() u 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; } 

Izlaz

 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. 

funkcija compare().

Funkcija compare() unaprijed je definirana funkcija biblioteke jezika C++. Funkcija compare() uspoređuje dva zadana niza i vraća sljedeće rezultate na temelju podudarnih slučajeva:

  1. Ako su oba niza ista, funkcija vraća 0.
  2. Ako je vrijednost znaka prvog niza manja od drugog niza, funkcija vraća<0.< li>
  3. Ako je drugi niz veći od prvog niza, funkcija vraća više od 0 ili >0.

Sintaksa

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

Kreirajmo jednostavan program za usporedbu dva niza pomoću funkcije compare() u C++.

Program2.cpp

 #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)>

Izlaz

 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. 

Relacijski operator

To je operator koji se koristi za usporedbu dva niza ili numeričkih vrijednosti u C++. C++ ima različite vrste relacijskih operatora kao što su '==', '!=', >,

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

Izlaz

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

2ndIzvršenje:

primjer json formata
 Enter the String 1: Program Enter the String 2: Program String is equal. 

Usporedite dva niza koristeći relacijski operator Not Equal To (!=).

Kreirajmo program za usporedbu jesu li nizovi jednaki ili ne koristeći operator Not Equal To (!=) u 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; } 

Izlaz

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

2ndTrčanje:

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

Usporedite dva niza koristeći for petlju i if naredbu u 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])>

Usporedite dva niza koristeći korisnički definiranu funkciju u C++

Kreirajmo jednostavan program za usporedbu prvog niza s drugim nizom pomoću korisnički definirane funkcije u 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; } 

Izlaz

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