strcmp() :(String compare)
This function helps us to compare two strings. The comparison of string is done character by character on the basis of ASCII values of the characters.
Note: Length of the string is of no concern.
Syntax:
int a;
a=strcmp(string1,string2);
If(a==0)
both the strings are same/ equal
if(a>0)
string1>string2
if(a<0)
string1<string2
ASCII values:
A-Z: 65 to 90
a-z: 97 to 122
0-9: 48 to 57
Example:1.
n1=”abc”
n2=”Abc”
n1>n2
Example:2
n1=”aBb”
n2=”abc”
n2>n1
Example:3
n1=”abc”
n2=”abc”
Both are same
Example:4
n1=”b”;
n2=”abcdefgh”;
n1>n2
Example:5
n1=”ab”
n2=”abc”
n2>n1
#include<stdio.h> #include<string.h> int main() { char n1[20],n2[20]; int a; printf("Enter 1st string "); gets(n1); printf("Enter 2nd string "); gets(n2); a=strcmp(n1,n2); if(a==0) printf("Both the strings are same/equal\n"); else if(a>0) printf("String %s is greater \n",n1); else printf("String %s is greater \n",n2); return(0); }
/* Output */ Enter 1st string abc Enter 2nd string ABC String abc is greater Enter 1st string ABC Enter 2nd string ABC Both the strings are same/equal
strcmpi(): (String compare ignoring case)
This function helps us to compare two strings ignoring case. The comparison of string is done character by character on the basis of ASCII values of the characters.
Note: Length of the string is of no concern.
Syntax:
int a;
a=strcmpi(string1,string2);
if(a==0)
both the strings are same/ equal
if(a>0)
string1>string2
if(a<0)
string1<string2
compares two strings without case sensitivity
Example:1
n1=”abc”
n2=”ABC”
a=strcmpi(n1,n2);
Result:
Both string are same/equal
#include<stdio.h> #include<string.h> int main() { char n1[20],n2[20]; int a; printf("Enter 1st string "); gets(n1); printf("Enter 2nd string "); gets(n2); a=strcmpi(n1,n2); if(a==0) printf("Both the strings are same/equal\n"); else if(a>0) printf("String %s is greater \n",n1); else printf("String %s is greater \n",n2); return(0); }
/* Output */ Enter 1st string hello Enter 2nd string HELLO Both the strings are same/equal
strncmp() :(compare first “n” characters of 2 strings)
This functions helps us to perform a comparison of two string with case sensitivity and of specified number of characters.
syntax:
int a;
a=strncmp(s1,s2,n);
compares first “n” characters of two strings with case sensitivity
#include<stdio.h> #include<string.h> int main() { char n1[20],n2[20]; int a; printf("Enter 1st string "); gets(n1); printf("Enter 2nd string "); gets(n2); a=strncmp(n1,n2,2); if(a==0) printf("Both the strings are same/equal\n"); else printf("Both the strings are not same/equal\n"); return(0); }
/* output */ Enter 1st string hello Enter 2nd string abc Both the strings are not same/equal Enter 1st string hello Enter 2nd string he232 Both the strings are same/equal Enter 1st string hello Enter 2nd string h3434 Both the strings are not same/equal
#include<stdio.h> #include<string.h> int main() { char n1[20],n2[20]; int a; printf("Enter 1st string "); gets(n1); printf("Enter 2nd string "); gets(n2); a=strncmp(n1,n2,2); if(a==0) printf("Both the strings are same/equal\n"); else if(a>0) printf("String %s is greater \n",n1); else printf("String %s is greater \n",n2); return(0); }
/* Output */ Enter 1st string hello Enter 2nd string hello Both the strings are same/equal Enter 1st string hello Enter 2nd string abc String hello is greater Enter 1st string hello Enter 2nd string he2345 Both the strings are same/equal