strcmp function >> string.h >> Compare two strings
What strcmp Actually Does ?
- Function takes two Strings as parameter
- 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 ); // Output -ve
Reason :
- ASCII value of "SAM" is smaller than "sam"
- 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 ); // Output +ve
Reason :
- ASCII value of "SAM" is greater than "sam"
- ASCII value of 'S' is greater than 's'
Note :
- This function is Case sensitive
- Header file : string.h

0 Comments:
Post a Comment
Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email