C Program to compare two strings without using strcmp


In this program, You will learn how to compare two strings without using strcmp in c.


1st String: Abc

2nd string: Bbc

Both strings are not equal

Example: How to compare two strings without using strcmp in c.

#include<stdio.h>
#include<string.h>

int main() {

    char str1[100], str2[100];
    int i = 0, f = 0, size1, size2;

    printf("Enter First String :");
    scanf("%[^\n]%*c", str1); 

    printf("Enter Second String :");
    scanf("%[^\n]%*c", str2); 

    size1 = strlen(str1);
    size2 = strlen(str2);

    if (size1 == size2) {

        while (str1[i] != '\0') {
            if (str1[i] != str2[i]) {
                f = 1;
                break;
            }
            i++;
        }
    } else {
        f = 1;
    }

    if (f == 1) {
        printf("Strings are Not Equal ");
    } else {
        printf("Strings are Equal ");
    }

    return 0;
}

Output:

Enter First String :Xiith.com
Enter Second String :Xiith.com
Strings are Equal