BUG ALERT !!
Prelude,
I found a bug (or atleast I think I have) in the substring function.
This was the last one suggeted by you :
Code:
const char *substr(const char *s, const char *match){
do {
const char *p, *q;
for (p = s, q = match; *p == *q; p++, q++)
if (*q == '\0') return s;
} while (*s++ != '\0');
return 0;
}
BUG :
art is a substr of artist and popart, but not of xxxartxxx, ie, substr returns true if it occurs either at the begining or the end only.
and I changed it to this (don't ask me why. My 6th sense quickly sensed some thing in it.(I'm not jocking)):
till now it woks fine.
Code:
const char *substr(const char *s, const char *match){
do {
const char *p, *q;
for (p = s, q = match; *p == *q; p++, q++)
if (*(q+1) == '\0') return s; //this line is what I changed...
} while (*s++ != '\0');
return 0;
}
and futher modified to...
Code:
int substr(const char *m, const char *s){
do {
const char *p, *q;
for (p = s, q = m; *p == *q; p++, q++)
if (*(q+1) == '\0') return 1;
}while (*s++ != '\0');
return 0;
}
I'm serious about the 6th sense thingi.
The moment I looked at it, my mind told me to tweak the if statement.
In fact I still don't know why it ( the fix ) works!!!