C Program to Compare Two Strings

In this article, you will learn and get code about checking whether the two input strings are equal or not with and without using standard library function.

Compare String in C without using strcmp() Function

This program will not use any standard library function say strcmp() that helps while comparing two string in C. Rather this program will compare the two given string by user with the help of self-defined code. Let's have a look at it:

#include<stdio.h>
#include<conio.h>
int main()
{
    char str1[50], str2[50];
    int i=0, chk=0;
    printf("Enter First String: ");
    gets(str1);
    printf("Enter Second String: ");
    gets(str2);
    while(str1[i]!='\0' || str2[i]!='\0')
    {
        if(str1[i]!=str2[i])
        {
            chk = 1;
            break;
        }
        i++;
    }
    if(chk==0)
        printf("\nStrings are Equal");
    else
        printf("\nStrings are not Equal");
    getch();
    return 0;
}

This program was compiled and executed in Code::Blocks IDE. On executing the above program, you will see the output as shown in the snapshot given below:

c program compare strings

Now supply any two string say codes and cracker and press ENTER key to see the output as shown here in the snapshot given below:

c program compare two strings

Let's take another sample run, in which let's suppose that user has provided two equal string say codes and codes.

compare two string program c

Let's take a look at some of the main logic used in above program.

Logic used in previous Program

Here are the list of some main logic used in previous program:

  • Scan both the string from user at run-time using gets() function
  • Store first and second string to str1 and str2 variable
  • Create a while loop, that runs until last character of both the string
  • Inside the loop, use if statement, to check whether the first character of first string is not equal to the first character of second string. If it evaluates to be true, then initialize 1 to chk and use break keyword, to terminate the loop
  • If condition of if block evaluates to be false, then the value of i gets incremented, and program flow goes back to the condition of while loop
  • This will continue, until the condition of while loop evaluates to be false, or the condition of if block (inside the while loop) evaluates to be true
  • After terminating the loop, check whether chk holds 0 or not. If it holds, then statements of if block never run. So both strings are equal
  • Otherwise, if chk holds 1, then statement of if block executed, so both strings are not equal. That's it

For example, let's suppose that if user enters codes and cracker as two input string. So the dry run of the above program using these two given string (as input) is given below:

  • Program declares two variables namely str1 and str2 of char type that can hold upto 50 characters
  • Program declares another two variables namely i and chk with 0 as its initial value
  • Program will scans two strings as input using gets() function
  • If user enters any string that will be of size less than 50, say codes, then a null terminated character (\0) automatically gets assigned after the last character (s) of the given string (codes). That is, if user supplies codes as first string, then
    • str1[0] holds c
    • str1[1] holds o
    • str1[2] holds d
    • str1[3] holds e
    • str1[4] holds s
    • str1[5] holds \0
  • Program checks the condition of while loop
  • At first run, condition of while loop,
    str1[i]!='\0' || str2[i]!='\0'
    or
    str1[0]!='\0' || str2[0]!='\0'
    or
    'c'!='\0' || 'c'!='\0'
    evaluates to be true. Because in both cases, c is not equal to null terminated character \0
  • So the program flow goes inside the while loop
  • Program checks the condition of if block, that is,
    str1[i]!=str2[i]
    or
    c!=c
    evaluates to be false, so program flow does not goes inside the if block
  • Program increments the value of i and goes back to the condition of while loop again
  • Program continue running until the condition of the while loop evaluates to be false
  • Or inside the loop, if the condition of if block evaluates to be true, then program flow goes inside the if block and 1 gets initialized to chk variable. And using break keyword, program flow goes out of the while loop or the loop gets terminated
  • And checks whether chk holds its original value (0) or not. If it holds, then if block's condition never evaluates to be true, so no any mismatched to character occurred from same position of both the string
  • So the program prints as Strings are Equal as output. Otherwise, prints Strings are not Equal

Compare String in C using Library Function

Let's create another simple program that that also checks whether the given two strings are equal or not using standard library function of C:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str1[50], str2[50];
    int len1, len2;
    printf("Enter First String: ");
    gets(str1);
    printf("Enter Second String: ");
    gets(str2);
    len1 = strlen(str1);
    len2 = strlen(str2);
    if(len1==len2)
    {
        if(strcmp(str1, str2)==0)
            printf("\nStrings are Equal");
        else
            printf("\nStrings are not Equal");
    }
    else
        printf("\nStrings are not Equal");
    getch();
    return 0;
}

This program will produce the same output as of previous program. In above program, first we have checked whether the length of both the string is equal or not, if it is equal then process further. Otherwise if length of the given two strings is not equal, then obviously both the strings will not be equal.

Note - If both strings are equal, then strcmp() function returns 0. Otherwise, if both strings are not equal, then strcmp() function returns 1

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋