Contact Learn C
Copy

Program 92:Compare two strings

Program 92:Compare two strings
#include<stdio.h>
#include<string.h>
main()
{
int i=0,flag=0,len1,len2;
char str1[100],str2[100];
printf("Enter a string 1\n");
gets(str1);
printf("Enter a string 2\n");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);

while(i<len1&&i<len2)
{
    if(str1[i]==str2[i])
    {
        i++;
        continue;
    }
    if(str1[i]<str2[i])
    {
        flag=-1;
        break;
    }
    if(str1[i]>str2[i])
    {
        flag=1;
        break;
    }
}
if(flag==0)
{
    printf("Both strings are equal\n");
}
if(flag==-1)
{
    printf("String1 is less than string 2\n");
}
if(flag==1)
{
    printf("String 1 is greater than string 2\n");
}
}
Explanation:

  1. This program starts with initializing :
    • len1,len2→ To store length of 2 strings
    • i →Used as helping variable and initialized to 0
    • flag →Used as boolean variable initialized to 0
    • str1, str2→ To store input strings from user
  2. printf("Enter a string 1\n");
    gets(str1);
    printf("Enter a string 2\n");
    gets(str2);
    Taking 2 strings from user.Let them be str1= Hello and str2=Hey 
  3. len1=strlen(str1);
    len2=strlen(str2);
    
    To store length of 2 strings.From here str1(Hello) length is len1=5 and str2(Hey) length is len2=3 
  4. Here to compare 2 strings ,First we need to compare 1st letters of str1 and str2,if they are equal then we will compare 2nd letters of str1 and str2 and so on till the end of any one of string.
    For Example Letter H of str1 is compared with H of str2 as they are equal
    We move on to compare 2nd letter which is E in str1 and E in str2,again both are equal
    Then we have to compare 3rd letter so now L of str1 != Y of str2.And as L<Y then output is
    string1 is less than string 2 
  5.  Now lets check how the above is happening in a program.Main logic of Program.i is initialized to 0 and len1=5 ,len2=3
    while(i<len1&&i<len2)
    {
        if(str1[i]==str2[i])
        {
            i++;
            continue;
        }
        if(str1[i]<str2[i])
        {
            flag=-1;
            break;
        }
        if(str1[i]>str2[i])
        {
            flag=1;
            break;
        }
    }
    • Iteration 1:0<5 &&0<3 so output is true so while loop is executed
      • Now the 1st if part will be checked.sr1[0]==str2[0]→H==H so true then i is incremented by1 and the loop will be redirected to forloop without executing remaining statements in while loop due to continue; statement
      • Now i=1
    • Iteration 2:1<5 &&1<3 so output is true so while loop is executed
      • Now the 1st if part will be checked.sr1[1]==str2[1]→e==e so true then i is incremented by1 and the loop will be redirected to forloop because of continue statement
      • Now i=2
    • Iteration 3:2<5 &&2<3 so output is true so while loop is executed
      • Now the 1st if part will be checked.sr1[2]==str2[2]→l==y which is false as l!=y
      • Now 2nd if part will be checked and str1[2]<str2[2]→l<y which is true and -1 is assigned to flag.
      • flag=-1
      • The loop will break and come out of for loop due to break statement.
  6. if(flag==0)
    {
        printf("Both strings are equal\n");
    }
    if(flag==-1)
    {
        printf("String1 is less than string 2\n");
    }
    if(flag==1)
    {
        printf("String 1 is greater than string 2\n");
    }
    }
    As flag=-1 the 2nd if part is executed which will print string1 is less than string2

Output:

Compare two strings


Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide