STRSTR Implementation in C
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
Returns a pointer to the first occurrence of str2 in str1, or a
null pointer if str2 is not part of str1.
The matching process does not include the terminating
null-characters, but it stops there.
#include
char *mystrstr(const char *str1, const char *str2){
char *a, *b;
while(*str1){
a = str1;
b = str2;
while(*a && *b && (*a == *b)) a++, b++;
if(*b == '\0') return a;
str1++;
}
return NULL;
}
int main(){
char *a = "Andaman and Nicobar is a beautiful island";
char *b = "and ";
char *res = mystrstr(a,b);
if(res)
printf("%s\n", res);
else
printf("Substring not found\n");
return 0;
}
//Output: Nicobar is a beautiful island
No comments:
Post a Comment