strcmp function : string.h >> Compare two strings



strcmp function >> string.h >> Compare two strings

What strcmp Actually Does ?

  1. Function takes two Strings as parameter
  2. It returns integer .

What strcmp Actually Does ?

  1. Function takes two Strings as parameter
  2. It returns integer .

Syntax :

int strcmp ( char *s1, char *s2 ) ;

Return Type

Return Type Condition
-ve Value String1 < String2
+ve Value String1 > String2
0 Value String1 = String2

Live Example 1 : Two strings are Equal 

char s1[10] = "SAM",s2[10]="SAM" ;

int len;
len = strcmp (s1,s2);

if (len == 0)
   printf ("Two Strings are Equal");

Live Example 2 : String1 is Greater than String2

char s1[10] = "SAM",s2[10]="sam" ;

int len;
len = strcmp (s1,s2);

printf ("%d",len); //-ve value

Reason  : 

  1. ASCII value of “SAM” is smaller than “sam”
  2. ASCII value of ‘S’ is smaller than ‘s’

Live Example 3 : String1 is Smaller than String1

char s1[10] = "sam",s2[10]="SAM" ;

int len;
len = strcmp (s1,s2);

printf ("%d",len); //+ve value

Reason  : 

  1. ASCII value of “SAM” is greater than “sam”
  2. ASCII value of ‘S’ is greater than ‘s’

Note :

  1. This function is Case sensitive
  2. Header file : string.h