/*C++/CPP/C program to
compare two strings without using library function strcmp*/
#include<stdio.h>
#include<string.h>
int strCmp(char *p1,
char *p2);
int main()
{
char arr1[20];
printf("Enter a
String:\n");
scanf("%s",&arr1);
char arr2[20];
printf("Enter a
String:\n");
scanf("%s",&arr2);
printf("You
entered:%s and %s\n",arr1,arr2);
int result;
result=strCmp(arr1,
arr2);
if(result==0)
printf("Equal\n");
else
printf("Not
Equal.\n");
return 0;
}
int strCmp(char *p1,
char *p2)
{
while(*p1++==*p2++)
{
if(*p1=='\0'|| *p2=='\0')
{
break;
}
}//end of while
if(*p1=='\0'
&& *p2=='\0')
{
return
0;
}
else
{
printf("else\n");
return -1;
}
}
/*
* $ gcc strCmp.c
$ ./a.out
Enter a String:
India
Enter a String:
Indi
You entered:India
and Indi
else
Not Equal.
$ ./a.out
Enter a String:
Indi
Enter a String:
India
You entered:Indi and
India
else
Not Equal.
$ ./a.out
Enter a String:
India
Enter a String:
India
You entered:India
and India
Equal
*/
No comments:
Post a Comment