Compare two strings using pointers

C program to compare two strings using pointers


Program

#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
   char str1[MAX], str2[MAX];
    char *ptr1, *ptr2;
    int result;

    printf("Enter first string\t:");
    gets(str1);
    printf("Enter second string\t:");
    gets(str2);

    ptr1 = str1;
    ptr2 = str2;

    while(*ptr1 == *ptr2)
    {
        if(*ptr1=='\0' && *ptr2=='\0')
        {
            break;
        }
        ptr1++;
        ptr2++;
    }

    result = *ptr1 - *ptr2;

    if(result == 0)
    {
        printf("\nBoth the strings are equal");
    }
    else if(result<0)
    {
        printf("\nFirst string is lexicographically smaller than second.");
    }
    else
    {
        printf("\nFirst string is lexicographically greater than second.");
    }

    getch();
}

Output

********** Run1 **********
Enter first string      :welcome
Enter second string     :weldone

First string is lexicographically smaller than second.


********** Run2 **********
Enter first string      :welldone
Enter second string     :welcome

First string is lexicographically greater than second.


********** Run3 **********
Enter first string      :welcome
Enter second string     :welcome

Both the strings are equal

Explanation

In this program, two strings are taken as an input from the user and stored in some variable named say 'str1' and 'str2'. Two pointers 'ptr1' and 'ptr2' are taken that points to 'str1' and 'str2' respectively. 'ptr1' and 'ptr2' points at the first character of their respective strings using the statement
ptr1 = str1;
ptr2 = str2;

Now, Compare two strings character by character till an unmatched character is found or end of any string is reached. If an unmatched character is found then strings are not equal. Else if both strings reached their end then both strings are equal.

while(*ptr1 == *ptr2)
{
if(*ptr1=='\0' && *ptr2=='\0')
{
break;
}
ptr1++;
ptr2++;
}

In above while loop, characters from both the strings are compared.
If characters are same, then, they are checked for NULL value.
If both the characters are NULL, then both the strings came to end. Hence, control comes out of the while loop, using break; statement.
If both the characters are not NULL, then both the pointers are incremented so that they point to the next characters to be checked.
If characters are not same, then while loop is terminated, stating that strings are not equal.

Outside the while loop, values in both the pointers are subtracted, so as to know whether the strings are equal or not
result = *ptr1 - *ptr2;
If result = 0, then it means strings are equal.
If result != 0, then strings are not equal.
if result < 0, then it means first string 'str1' is lexicographically smaller than second 'str2'.
else if result > 0, then it means first string 'str1' is lexicographically greater than second 'str2'.


We can optimize the above while loop in compare function to get little geeky.
First let us combine the while and if condition in a single condition.
while((*ptr1 != '\0' && *ptr2 != '\0') && (*ptr1 == *ptr2))
{
ptr1++;
ptr2++;
}
Next we know, in C programming NULL character is represented using 0. Hence, the above NULL checking condition can be stripped out.
while((*ptr1 && *ptr2) && (*ptr1 == *ptr2))
{
ptr1++;
ptr2++;
}
The program for the same is in "Method 2" tab.

Program

#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
    char str1[MAX], str2[MAX];
    char *ptr1, *ptr2;
    int result;

    printf("Enter first string\t:");
    gets(str1);
    printf("Enter second string\t:");
    gets(str2);

    ptr1 = str1;
    ptr2 = str2;

    while((*ptr1 && *ptr2) && (*ptr1 == *ptr2))
    {
        ptr1++;
        ptr2++;
    }

    result = *ptr1 - *ptr2;

    if(result == 0)
    {
        printf("\nBoth the strings are equal");
    }
    else if(result<0)
    {
        printf("\nFirst string is lexicographically smaller than second.");
    }
    else
    {
        printf("\nFirst string is lexicographically greater than second.");
    }

    getch();
}

Output

********** Run1 **********
Enter first string      :welcome
Enter second string     :weldone

First string is lexicographically smaller than second.


********** Run2 **********
Enter first string      :welldone
Enter second string     :welcome

First string is lexicographically greater than second.


********** Run3 **********
Enter first string      :welcome
Enter second string     :welcome

Both the strings are equal