C Program to compare two strings and check they are equal

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is string comparison?

In this c program, we need to check the two strings are equal or not; that means we need to compare each character in the strings if that is equal or not. First, we have to read two strings from the user and check each character of the string. If all the characters of the strings are equal, then we can display the strings are equal.

How is string comparison implemented in C?

For implementing this program, we have to declare two character arrays, 'str1[]' and 'str2[]', to store the user input strings. Declare two integer variables 'count1' and 'count2' to keep the length of the strings. After reading two strings from the user, using a 'while loop' with the condition  'str1[count1]!='\0'', increment 'count1' by 1.

Now the 'count1' variable contains the length of the string1. Use another 'while loop' with the condition 'str1[count2]!='\0'', increment 'count2' by 1, to get the length of string2. Variable 'count1' contains the number of characters in string1, and 'count2' contains the number of characters in string2.

We have to make the string comparison by checking the first character of each string and continues with the subsequent characters until the characters differ or until the end of the string is reached. For doing this, we have to assign 'i=0'. Then by using a 'while loop' with the condition (i < count1) && (i < count2),check, the characters of each string is equal or not ,(str1[i] == str2[i]).

If equal, then increment 'i' by 1 and continue the checking with the next character in the string. If the second string is greater than the first string makes the flag as -1. If  "str1[i] > str2[i]", the first string is greater than the second string then set 'flag = 1' and break from the loop. Then check the value of the flag, if 'flag==0' then display both the strings are equal. If 'flag==1', then display string1 is greater than string2.If flag==-1, then display string1 is less than string2.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the integer variables 'count1, count2, flag, i and set count1=0, count2=0, flag=0'.

STEP 3: Declare the two Arrays 'str1[], str2[]' as a character type.

STEP 4: Read String from the user and keep it in the variable str1 using the gets function.

STEP 5: Read another String from the user and keep it in the variable str2 using the gets function.

STEP 6: By using a while loop with the condition 'str1[count1]!='\0' ',increment 'count1' by one.

STEP 7: By using a while loop with the condition 'str2[count1]!='\0'' ,increment 'count2' by one.

STEP 8: Set the integer variable 'i=0'.

STEP 9: By using a while loop with the condition 'i < count1' and' i < count2' do step 6.

STEP 10: Check if 'str1[i]==str2[i]' then increment i by one and repeat the step 6.

STEP 11: If str1[i]

STEP 12: If 'str1[i] > str2[i]' then set flag=1 and goto step 14.

STEP 13: Repeat step 5.

STEP 14: Check if flag == 0 then display both strings are equal.

STEP 15: Check if flag == 1 then display 'string1' is greater than string2.

STEP 16: Check if flag == -1 then display 'string1' is less than string2.

C Source Code

                                          #include <stdio.h>
#include <string.h>
int main()
{
   char str1[10], str2[10];;
   int count1 = 0, count2 = 0, flag = 0, i;
   printf("Enter a string\n");
   gets(str1);

   printf("Enter a string\n");
   gets(str2);
   while (str1[count1] != '\0')
    count1++;

  while (str2[count2] != '\0')
    count2++;
  i = 0;
 
  while ((i < count1) && (i < count2))
  {
    if (str1[i] == str2[i]) /* checking the characters in string if it is equal or not */

    {
      i++;
      continue;
    }
    if (str1[i] < str2[i])
    {
      flag = -1; /* set flag minus one if string two greater than string one */
      break;
    }
    if (str1[i] > str2[i])
    {
      flag = 1; /* set flag one if string one is greater than string two */
      break;
    }
  }
  if (flag == 0)
    printf("Both strings are equal\n");
  if (flag == 1)
    printf("String1 is greater than string2\n", str1, str2);
  if (flag == -1)
    printf("String1 is less than string2\n", str1, str2);

}
                                      

OUTPUT

Enter a string:
happy

Enter another string:
HAPPY

String1 is greater than string2
RUN2

Enter a string:
Hello

Enter another string:
Hello

Both strings are equal
RUN3

Enter a string:
gold

Enter another string:
silver
string1 is less than string2