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

Run it Now
#include<stdio.h>
#include<string.h>

int main() {

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

    printf("Enter First String :");
    fgets(str1, 100, stdin);
    strtok(str1, "\n");

    printf("\nEnter Second String :");
    fgets(str2, 100, stdin);
    strtok(str2, "\n");

    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("\nStrings are Not Equal ");
    } else {
        printf("\nStrings are Equal ");
    }

    return 0;
}

Output:

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