Friday, June 20, 2014

String Comparison using pointers , functions , loops and if selection statements

You can comparison two strings in C Programming language by using built-in function defined in <String.h>. The back end code working behind the function in <String.h> library to comparison two strings works as follows:

#include<stdio.h>
int stcp(char *a , char *b)
{
    int i;
    while(*a==*b)
    {
        if (*a=='\0' || *b=='\0')
        break;
        a++;
        b++;

    }
    if (*a=='\0' && *b=='\0')
        return 0;
    else return -1;
}
int main()
{
    char a[20],b[20];
    printf("Enter String 1 : ");
    scanf("%s",a);
    printf("Enter String 2 : ");
    scanf("%s",b);
    if(stcp(a,b)==0)
    {
        printf("String 1 = String 2");
    }
    else if (stcp(a,b)==-1)
    {
        printf("String 1 is not matching string 2");
    }
}

No comments:

Post a Comment