C Program to compare Two String Using strcmp() Function

   

 

                     

       
Algorithm:
 
step 1:start
 
step 2:declare a,b as character array
 
step 3:input a,b 
 
step 4:perform strcmp() operation
 
step 5:print "Which string is greater"
 
step 6:stop
 
Flowchart:
 

                        
 

Code:

 #include <stdio.h>
int main()
{
    char a[100],b[100];
    printf("Enter 1st string:");
    scanf("%s",&a);
    printf("Enter 2nd string:");
    scanf("%s",&b);
    if(strcmp(a,b)==0)
    {
        printf("The Strings are Equal");
    }
    else if(strcmp(a,b)>0)
    {
        printf("%s String is greater",a);
    }
    else 
    {
        printf("%s String is greater",b);
    }
    return 0;
}

  

Sample Input and Output

Enter 1st string:Hello                                                                                                               
Enter 2nd string:Hi                                                                                                                  
Hi String is greater   



 Explanation of above program 

The main objective of this post is to give you clear idea of string comparison using strcmp() in C Programming  

I known most of you must be thinking that my program is giving wrong output because Hi cannot be greater than Hello but I would like to tell you that Hi is greater than Hello,Let's understand how? line by line! 

strcmp()

int strcmp(const char* X, const char* Y);  

The strcmp() function returns an integer greater than,equal to,or less than zero,accordingly as the string pointed to be X is greater than,equal to,or less than the  string pointed to by Y.

The function basically performs a binary comparison of both string' characters until they differ or until a terminating null character is  reached.

In the first line of code we are importing stdio.h header file which help us to use some inbuilt functions like printf(),scanf() etc(which are basic functions).In the next line,we are defining main function,main function is nothing mother of all other function,Every C Program starts its execution from main function only,In this program we have wrote int return type of main function so we should return integer value otherwise it will give compile time error,we can also write return type as void(no value) so we don't have return anything.

 After that we are declaring two array of character a[],b[] which we will use to store two string value respectively we have given array size of 100 in both character array(a,b) we can give any array size but it should be more than length of string which we will take as input for comparison of two string.In the next line we are simply printing the statement "enter 1st string" This message will give the idea to user that we are waiting for your input because after the print statement we are taking user input using scanf() function and storing it in a character array and as we can we are using %s format specifier to store character array.

 In the next line we are doing same thing for another character array(b) as we have done before for character array a[].After that we are using else if control structure for comparing if first string is greater than second or if second string is greater than first or to check whether they are equal or what.So finally the suspense of this article will get over now(How Hi is greater than Hello)Let's see!!.In the if statement we are checking whether strcmp(a,b)==0 The meaning of this statement is compare the ascii( American Standard Code For Information Interchange) value of character stored in both the variable i.e a,b.In the if block we are comparing (strcmp(a,b)==0 )this mean if the ascii value of all the character stored in both a,b is same then this block will get executed.

for eg a="Om"

           b="Om"

then strcmp(a,b) will return 0 and then if block get  executed.

In the else if block we have put strcmp(a,b)>=0  the meaning of this operation is that compare the ascii value of  corresponding character  stored in a,b and return once any corresponding character of a is higher than corresponding character of b and if this condition never satisfy then it will execute else block.

 


 

Let's See it with an example 

a="Ram"

b="Rahim"

In above eg the strcmp() will start comparing one by one character from both a and b in first iteration R and R character both are same so it will continue  checking next alphabet,Unfortunately in the second iteration as well we have same alphabet i.e a therefore we have to  check next character again,the third character of a is m and b is h so m is greater than h  because ASCII value of m is  109 and  h is 104 So we can see that a String  is greater than b string.

I would like to tell you  one important note that is  ASCII value of A is not equal a I mean ASCII value of Capital Alphabet and Small Alphabet are totally different 

ASCII Value of A-Z(Capital Alphabets) is  65-90


ASCII Value of a-z(Small Alphabets) is  65-90

The else block will be executed when the two string are not equal or the first string is not greater than second string.So we can say that if else block is executed then the 2nd string is greater than the first string

  Lets's understand with an example

   a="Hi"

   b="Hello"

 In above example H and H are same in both the character array so we will check next character the next character is i and e so we known that the ASCII value of i is greater than the ASCII value of e Therefore a is greater  than b therefore Hi is greater than Hello.

If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

 


 



 

 

 

 

 


 

Previous Next

2 Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. strcmp is O(n) operation and will be expensive if the strings are really long. You are doing twice in the same program. try assigning the result of the first strcmp to an integer. like this:

    #include
    int main()
    {
    char a[100],b[100];
    printf("Enter 1st string:");
    scanf("%s",&a);
    printf("Enter 2nd string:");
    scanf("%s",&b);
    int jc = strcmp(a,b);
    if(jc==0)
    {
    printf("The Strings are Equal");
    }
    else if(jc>0)
    {
    printf("%s String is greater",a);
    }
    else
    {
    printf("%s String is greater",b);
    }
    return 0;
    }

    ReplyDelete
Previous Post Next Post