#include <stdio.h>
#include <string.h>
/* check if s1 ends with s2; return 1 if match else 0 */
int
endswith(char *s1, char *s2)
{
char *s1end, *s2end;
s1end = s1 + strlen(s1) + 1;
s2end = s2 + strlen(s2) + 1;
while (--s2end >= s2 && --s1end >= s1) {
if (*s1end != *s2end)
return 0;
}
if (s2end < s2)
return 1;
return 0;
}
int
main()
{
char* filespec[] = { "New Powerbooks next Tuesdayxt",
"I like snails.txt, I really do",
"realfile.txt",
"autoexec.tx",
"txt",
"this is text.txt",
".txt",
"x",
""
};
int i;
for (i=0; *filespec[i]; i++) {
printf("%s: %s\n", filespec[i],
endswith(filespec[i], ".txt") ? "yes" : "no");
}
return 0;
}