Tuesday, September 18, 2012

C program to compare two strings without using library function

// c program to compare two strings without using library functions
#include <stdio.h>
int main()

{
char a[50];
char b[50];
int i, j, nr1=0, nr2=0;
printf("enter the two strings you want to compare\n");
scanf("%s %s", a, b);
//counting the length of each string
while(a[i]!=\0)
{
nr1++;
i++;
}
while(b[j]!=\0)
{
nr2++;
j++;
}
if(nr1 != nr2)
{
printf("NO different lengths");
return 0;
}

for(i=0;i<nr1;i++)
{if(a[i]!=b[j])
{ printf("NO");
return0;
}
}
printf("the strings are identic");

}



No comments:

Post a Comment