C Program to compare two strings using pointers


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


1st String: Abc

2nd string: Bbc

Both strings are not equal

Example: How to compare two strings using pointers in c.

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

int main() {

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

    char *ptr1 = str1;
    char *ptr2 = str2;

    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 (*(ptr1 + i) != '\0') {
            if (*(ptr1 + i) != *(ptr2 + 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