Right Click to Search

Friday, January 22, 2025

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 .
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  : 
  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 );           // Output +ve

Reason  : 
  1. ASCII value of "SAM" is greater than "sam" 
  2. ASCII value of 'S' is greater than 's'  
Note :
  • This function is Case sensitive
  • Header file  :  string.h

Bookmark & Share

Tags / Keywords : | , ,

Stumble
Delicious
Technorati
Twitter
Facebook

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

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates