C Program to Compare Two Strings

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

Compare strings in C without using the strcmp() function

This program will not use any standard library function, such as strcmp(), that helps while comparing two strings in C. Rather, this program will compare the two given strings with the help of a 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 the 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 strings, say "codes" and "cracker", and press the 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 the user has provided two equal strings, say codes and codes.

compare two string program c

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

Logic used in previous Program

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

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

Using a Library Function to Compare Strings in C

Let's create another simple program that also checks whether the given two strings are equal or not using the standard library function 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 the previous program. In the above program, first we have checked whether the length of both strings is equal or not; if it is equal, then we process further. If the lengths of the two given strings are not equal, neither string will be equal.

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

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this article? Share it!





Like/follow us on social media for updates!




Like/follow us on social media for updates!