C Program to match Two String Utilizing strcmp() Operate

[ad_1]

 

Code:

 #embody <stdio.h>

int most important()

{

    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 higher”,a);

    }

    else 

    {

        printf(%s String is higher”,b);

    }

    return 0;

}

  

Pattern Enter and Output

Enter 1st string:Hi there                                                                                                               

Enter 2nd string:Hello                                                                                                                  

Hello String is higher   

 Rationalization of above program 

The primary goal of this put up is to present you clear thought of string comparability utilizing strcmp() in C Programming  

I identified most of you should be considering that my program is giving incorrect output as a result of Hello can’t be higher than Hi there however I wish to inform you that Hello is bigger than Hi there,Let’s perceive how? line by line! 

strcmp()

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

The strcmp() perform returns an integer higher than,equal to,or lower than zero,accordingly because the string pointed to be X is bigger than,equal to,or lower than the  string pointed to by Y.

The perform mainly performs a binary comparability of each string’ characters till they differ or till a terminating null character is  reached.

Within the first line of code we’re importing stdio.h header file which assist us to make use of some inbuilt features like printf(),scanf() and so on(that are fundamental features).Within the subsequent line,we’re defining most important perform,most important perform is nothing mom of all different perform,Each C Program begins its execution from most important perform solely,On this program we’ve wrote int return sort of most important perform so we must always return integer worth in any other case it is going to give compile time error,we will additionally write return sort as void(no worth) so we do not have return something.

 After that we’re declaring two array of character a[],b[] which we are going to use to retailer two string worth respectively we’ve given array dimension of 100 in each character array(a,b) we may give any array dimension however it needs to be greater than size of string which we are going to take as enter for comparability of two string.Within the subsequent line we’re merely printing the assertion “enter 1st string” This message will give the thought to person that we’re ready to your enter as a result of after the print assertion we’re taking person enter utilizing scanf() perform and storing it in a personality array and as we will we’re utilizing %s format specifier to retailer character array.

 Within the subsequent line we’re doing identical factor for an additional character array(b) as we’ve performed earlier than for character array a[].After that we’re utilizing else if management construction for evaluating if first string is bigger than second or if second string is bigger than first or to test whether or not they’re equal or what.So lastly the suspense of this text will recover from now(How Hello is bigger than Hi there)Let’s have a look at!!.Within the if assertion we’re checking whether or not strcmp(a,b)==0 The that means of this assertion is evaluate the ascii( American Commonplace Code For Info Interchange) worth of character saved in each the variable i.e a,b.Within the if block we’re evaluating (strcmp(a,b)==0 )this imply if the ascii worth of all of the character saved in each a,b is identical then this block will get executed.

for eg a=”Om”

           b=”Om”

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

Within the else if block we’ve put strcmp(a,b)>=0  the that means of this operation is that evaluate the ascii worth of  corresponding character  saved in a,b and return as soon as any corresponding character of a is larger than corresponding character of b and if this situation by no means fulfill then it is going to execute else block.

 


 

Let’s See it with an instance 

a=”Ram”

b=”Rahim”

In above eg the strcmp() will begin evaluating one after the other character from each a and b in first iteration R and R character each are identical so it is going to proceed  checking subsequent alphabet,Sadly within the second iteration as properly we’ve identical alphabet i.e a due to this fact we’ve to  test subsequent character once more,the third character of a is m and b is h so m is bigger than h  as a result of ASCII worth of m is  109 and  h is 104 So we will see {that a} String  is bigger than b string.

I wish to inform you  one essential word that’s  ASCII worth of A will not be equal a I imply ASCII worth of Capital Alphabet and Small Alphabet are completely completely different 

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

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

The else block can be executed when the 2 string are usually not equal or the primary string will not be higher than second string.So we will say that if else block is executed then the 2nd string is bigger than the primary string

  Lets’s perceive with an instance

   a=”Hello”

   b=”Hi there”

 In above instance H and H are identical in each the character array so we are going to test subsequent character the subsequent character is i and e so we identified that the ASCII worth of i is bigger than the ASCII worth of e Due to this fact a is bigger  than b due to this fact Hello is bigger than Hi there.

If you happen to any have doubt,suggestion relating to this put up or web site then be happy to share with us.

 

You probably have discovered one thing new from this put up then share this put up with your loved ones and
pals.

 

Blissful Studying :)?  

 

 

 

 

 

 

 

 

Earlier
Subsequent

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *