Sunday, 20 March 2016

C Program For Compare Two String Using Pointer

#include<stdio.h>
int main(){

    char string1[50],string2[50],*str1,*str2;
    int i,equal = 0;

    printf("Enter first string: ");
    scanf("%s",string1);

    printf("Enter second string: ");
    scanf("%s",string2);

    str1 = string1;
    str2 = string2;

    while(*str1 == *str2)
    {

        if ( *str1 == '\0' || *str2 == '\0' )
            break;

        str1++;
        str2++;

    }

    if( *str1 == '\0' && *str2 == '\0' )
        printf("Both strings are equal.");

    else
        printf("Both strings are not equal");

}

Output:-


No comments:
Write comments

Recommended Posts × +