Saturday, October 1, 2022
HomeC ProgrammingC Program to check Two String Utilizing strcmp() Operate

C Program to check Two String Utilizing strcmp() Operate


 

Code:

 #embrace <stdio.h>

int 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:Whats up                                                                                                               

Enter 2nd string:Hello                                                                                                                  

Hello String is higher   

 Rationalization of above program 

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

I recognized most of you have to be considering that my program is giving flawed output as a result of Hello can’t be higher than Whats up however I want to inform you that Hello is larger than Whats up,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 larger than,equal to,or lower than the  string pointed to by Y.

The perform principally 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 capabilities like printf(),scanf() and so forth(that are primary capabilities).Within the subsequent line,we’re defining important perform,important perform is nothing mom of all different perform,Each C Program begins its execution from important perform solely,On this program we’ve got wrote int return sort of important perform so we must always return integer worth in any other case it’s 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’ll use to retailer two string worth respectively we’ve got given array measurement of 100 in each character array(a,b) we can provide any array measurement however it needs to be greater than size of string which we’ll 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 concept to person that we’re ready on 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 got performed earlier than for character array a[].After that we’re utilizing else if management construction for evaluating if first string is larger than second or if second string is larger 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 larger than Whats up)Let’s examine!!.Within the if assertion we’re checking whether or not strcmp(a,b)==0 The that means of this assertion is examine the ascii( American Normal 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 got put strcmp(a,b)>=0  the that means of this operation is that examine 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’s 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’s going to proceed  checking subsequent alphabet,Sadly within the second iteration as effectively we’ve got identical alphabet i.e a due to this fact we’ve got to  test subsequent character once more,the third character of a is m and b is h so m is larger than h  as a result of ASCII worth of m is  109 and  h is 104 So we will see {that a} String  is larger than b string.

I want to inform you  one essential observe 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 might be executed when the 2 string will not be 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 larger than the primary string

  Lets’s perceive with an instance

   a=”Hello”

   b=”Whats up”

 In above instance H and H are identical in each the character array so we’ll test subsequent character the subsequent character is i and e so we recognized that the ASCII worth of i is larger than the ASCII worth of e Subsequently a is larger  than b due to this fact Hello is larger than Whats up.

When you any have doubt,suggestion relating to this put up or web site then be at liberty to share with us.

 

When you’ve got discovered one thing new from this put up then share this put up with your loved ones and
pals.

 

Completely satisfied Studying :)😎  

 

 

 

 

 

 

 

 

Earlier
Subsequent

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments