Solved programming problem

Comparing two string with pointer in C.

Source code

#include <stdio.h>

int compare(char *p1,char *p2) /// receiving the pointer from the main function

{

    int i=0,flag=0;

    while(*(p1+i)!='' || *(p2+i)!='') ///checking the string while NULL

    {

        if(*(p1+i)==*(p2+i))

        {

            flag=1;

        }

        else     ///if they are not same,then makes the  flag=0 and break

        {

            flag=0;

            break;

        }

        i++;

    }

    if(flag==1)

    {

        printf("
 Match!!!
");

    }

    else printf("
 Don't Match!!!!!!
");

}

int main()

{

    char str1[100],str2[100];

    int i;

    char *p1,*p2;

    printf("Please Enter The 1st String: ");

    gets(str1);

    printf("Please Enter The 2nd String: ");

    gets(str2);

    p1=&str1; ///saving the address in pointer

    p2=&str2; ///saving the address in pointer

    compare(p1,p2);

    return 0;

}

Output

Please Enter The 1st String: code to haven
Please Enter The 2nd String: code to haven

 Match!!!

 

Join the discussion

Add your comment