Compare two string without using strcmp()

In this tutorial, we are going to learn C Program to Compare Two Strings Without Using strcmp().

Using string function it is easy to compare two string we only need to write only one function. But we can also compare string without using string function.

How to compare two strings without using strcmp():

For this comparison of string , we use simple relational operators  (== , > , < ) . Using this operator we compare two strings.

== operator will compare two string and returns 0

< operator will compare two string and returns value -1 if less

> operator will compare two string and returns 1 value if greater

Example:

H == H => 0 => both equal

H < J => -1 => H is less than J

J > I => 1 => J is greater than I

Algorithm:

  1. Start
  2. Declare variable
  3. Take two input string from the user
  4. Read a character from both string one by one.
  5. Compare character using relational operators.
  6. If == , print both are equal
  7. If < , print less
  8. If > , print greater.
  9. Stop

C program to Compare Two Strings without using strcmp()

1234567891011121314151617181920212223242526272829303132333435363738394041#include <stdio.h>#include <conio.h>void main (){  // declare variables   char str1 [30], str2 [30];   int i = 0, flag=0 ,len1, len2;     // take two string input   printf (“Enter string1”);  gets (str1);  printf (“\n Enter string2”);  gets (str2);  //length of both string    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 (“\n Both strings are equal “);  if(flag == -1)    printf (“\n string1 is less than string2 “);  if( flag == 1)    printf (“\n string1 is greater than string2 “);}

Output:

Compare two string without string function

Explanation :-

1. First we declare variable

str1 => for string 1 input

str2 => for string 2 input

i => for iteration

flag => for storing the value the comparison returns

len1  => for storing length of string 1

len2 => for storing length of string 2

2. Take input 2 string from user.

str1 = technicalseek

str2 = technicalseek

3. Now calculate length of both the strings

stelen (): is string function which returns the length of the string

Example : strlen (“abc”);

It will return length 3.

In our input len1 = 13 and len2 = 13.

4. Next lines

while (i < len1 && i < len2 )

{

}

This is to traverse both the strings till length (end )

Next lines are

if( str1 [i] == str2 [i])

{

i++;

continue;

}

As we traverse using while loop we check above condition to check whether string are equal.

At this step, the one character from str1 and one character from str2 are compared.

For out input : technicalseek,  str2= technicalseek

First iteration i=0

str1[0] == str2[0] => t == t

It will become true so it move to next character.

Second iteration i = 1

str1 [1] == str2 [1] =>e == email

It will become true so it move to next character.

This will continue until the length of both strings.

In the end, flag = 0 remains same in out case.

5. Another conditions

if( str1 [i] < str2 [i])

{

flag = -1;

break;

}

This is another condition to compare strings.

Example :- string1 = Live , string 2 = Site

In this case

First iteration i=0

str1[0] == str2[0] => L < S

This condition become true so flag = -1 and breaks the loop.

Another conditions

if( str1 [i] > str2 [i])

{

flag = 1;

break;

}

This is another condition to compare strings.

Example: string1 = Site , string 2 = Live

In this case

First iteration i=0

str1[0] == str2[0] => S < L

This condition become true so flag = 1 and breaks the loop.

6. Now, check the flag value.

In our input string and after comparing the string the flag = 0

So, print both strings are equal.

7. Stop.