C ++ strcmp () - Libreria standard C ++

La funzione strcmp () in C ++ confronta due stringhe di terminazione null. Il confronto viene effettuato lessicograficamente.

prototipo strcmp ()

 int strcmp (const char * lhs, const char * rhs);

La strcmp()funzione accetta due argomenti: lhs e rhs. Confronta lessicograficamente i contenuti di lhs e rhs. Il segno del risultato è il segno della differenza tra le prime coppie di caratteri che differiscono per lhs e rhs.

Il comportamento di strcmp()non è definito se lhs o rhs non puntano a stringhe con terminazione nulla.

È definito nel file di intestazione "> file di intestazione.

Parametri strcmp ()

lhs and rhs: Puntatore alle stringhe con terminazione null da confrontare.

strcmp () Restituisce il valore

La funzione strcmp () restituisce:

  • valore positivo se il primo carattere diverso in lhs è maggiore del carattere corrispondente in rhs.
  • valore negativo se il primo carattere diverso in lhs è minore del carattere corrispondente in rhs.
  • 0 se hs e rhs sono uguali.

Esempio: come funziona la funzione strcmp ()

 #include #include using namespace std; void display(char *lhs, char *rhs, int result) ( if(result> 0) cout << rhs << " precedes " << lhs << endl; else if (result < 0) cout << lhs << " precedes " << rhs << endl; else cout << lhs << " and " << rhs << " are same" << endl; ) int main() ( char lhs() = "Armstrong"; char rhs() = "Army"; int result; result = strcmp(lhs,rhs); display(lhs,rhs,result); result = strcmp(lhs,lhs); display(lhs,lhs,result); return 0; )

Quando esegui il programma, l'output sarà:

 Armstrong precede l'esercito Armstrong e Armstrong sono gli stessi

Articoli interessanti...