The string I'd like to check is something like "abcSun24def". If any valid "xxxyy" (xxx = weekday and yy = day) is found, return the position inside the string. If "xxxyy" is not found, return -1.
The code works as desired, but I think it can be optimized.
/* -------------------------------------------------------------
FUNC : findxy (find pattern xxxyy)
xxx = weekday (e.g. "Mon01")
yy = day
roster specific formatting
PARAMS : c (char *), pointer to string
RETURNS : (int), if pattern found, pointer to found pattern in string c
-1 if pattern not found
REMARKS :
---------------------------------------------------------------- */
int findxy(char *c) {
const char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int i, j;
char bufw[4];
char bufd[3];
/* check if c is at least 5 chars long */
if (strlen(c) < 5)
return -1;
for (i = 0; i <= (int)strlen(c)-5; i++) {
memcpy(bufw, c+i, 3);
bufw[3]='\0';
/* check all 7 weekdays */
for (j = 0; j < 7; j++) {
/* find weekday matches */
if (!strcmp(bufw, days[j])) {
/* check if both chars following weekday are numerical */
if (isdigit(c[i+3]) && isdigit(c[i+4])) {
memcpy(bufd, c+i+3, 2);
bufd[2]='\0';
/* check if number after weekday is a valid day */
if (atoi(bufd) >= 1 && atoi(bufd) <= 31) {
return i;
}
}
}
}
}
return -1;
}