Hello everyone,
I am eager to know about string functions, (user defined) . tell me
the technique of find a string in another string.
/* The following is a public-domain implementation of the strstr()
function and the functions it calls in the <string.h> header. */
#include <string.h>
/* memcmp */
int (memcmp)(const void *s1, const void *s2, size_t n)
{
unsigned char *us1 = (unsigned char *) s1;
unsigned char *us2 = (unsigned char *) s2;
while (n-- != 0) {
if (*us1 != *us2)
return (*us1 < *us2) ? -1 : +1;
us1++;
us2++;
}
return 0;
}
/* strchr */
char *(strchr)(const char *s, int c)
{
/* Scan s for the character. When this loop is finished,
s will either point to the end of the string or the
character we were looking for. */
while (*s != '\0' && *s != c)
s++;
return ( (*s == c) ? (char *) s : NULL );
}
/* strlen */
size_t (strlen)(const char *s)
{
char *p = strchr(s, '\0');
return (p - s);
}
/* strstr */
char *(strstr)(const char *s1, const char *s2)
{
size_t s2len;
/* Check for the null s2 case. */
if (*s2 == '\0')
return (char *) s1;
s2len = strlen(s2);
for (; (s1 = strchr(s1, *s2)) != NULL; s1++)
if (memcmp(s1, s2, s2len) == 0)
return (char *) s1;
return NULL;
}
/* Hopefully this helps ... Gregory Pietsch */