Self-defined function using pointers(Selfcmp())
Important Question:
C program using pointers, write a user-defined function named “selfcmp()” to compare two string?
example:
1.
Given:
n1=”hello”
n2=”hello”
Output:
Both the string are same
2.
Given:
n1=”hello”
n2=”world”
Output:
String are not same
#include<stdio.h> #include<string.h> int selfcmp(char *p1,char *p2) { while(*p1==*p2) { if(*p1=='\0' && *p2=='\0') return(1); p1++; p2++; } return(0); } int main() { char n1[20],n2[20]; int z; printf("Enter first string "); gets(n1); printf("Enter second string "); gets(n2); printf("First string %s\n",n1); printf("Second string %s\n",n2); z=selfcmp(n1,n2); if(z==1) printf("String are same"); else printf("String are not same"); return(0); }
Output:
Enter first string hello
Enter second string hello
First string hello
Second string hello
String are same
Enter first string hello
Enter second string world
First string hello
Second string world
String are not same