string finding

P

pete

And the way I wrote memcmp() above, it doesn't.

I have a toy string library, as I suspect you do,
but I only use function calls in my function definitions
in cases where the real library functions would also do the trick.
My main goals are
1 trying to get the definitions correct
2 showing any relationships that may exist between
what various standard library functions do.

If you rewrite the above strstr function definition
with strncmp instead of memcmp,
then you are showing a relationship that really exists between
what the standard library functions strstr and strncmp do.

I don't think there's any real value
in showing that what strstr does,
can be described by your specific implementation of memcmp.
 
G

Gregory Pietsch

pete said:
I have a toy string library, as I suspect you do,
but I only use function calls in my function definitions
in cases where the real library functions would also do the trick.
My main goals are
1 trying to get the definitions correct
2 showing any relationships that may exist between
what various standard library functions do.

The string.h header is a hodgepodge. It represents not a concerted
design effort but rather the accretion of contributions made by various
authors over a span of years.
If you rewrite the above strstr function definition
with strncmp instead of memcmp,
then you are showing a relationship that really exists between
what the standard library functions strstr and strncmp do.

I could write it correctly with neither:

#include <string.h>
char *(strstr)(const char *s1, const char *s2)
{
const char *sc1, *sc2;

if (*s2 == '\0')
return (char *) s1; /* takes care of null s2 case */
while ((s1 = strchr(s1, *s2)) != NULL) {
sc1 = s1;
sc2 = s2;
while ( 1 ) {
/* match rest of prefix */
if (*++sc2 == '\0')
return (char *) s1;
else if (*++sc1 != *sc2)
break;
}
++s1;
}
return NULL;
}
I don't think there's any real value
in showing that what strstr does,
can be described by your specific implementation of memcmp.

I included it for completeness.

Gregory Pietsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top