Alternative solution
I wrote an alternative solution; I'm submitting it here in case someone wishes to put it on the main page. Corrections/comments/style fixes greatly appreciated. JesseW 20:00, 9 July 2007 (BST)
/* strend(char * s, char * t) -- return true if t ends s.*/ /* use pointers */ /* basic code outline (non-pointer version) copied from strcat shown in K&R, page 48 */ int mystrend(char *s, char *t) { //printf("mystrend(\"%s\", \"%s\")\n",s,t); char * start_of_t = t; while (*(s++) != '\0') /* find end of s */ ;//printf("!%c!",*s); //printf("\n"); while (*(t++) != '\0') /* find end of t */ ;//printf("!%c!",*t); //printf("\n(%i)==(%i):%i (%i)>=(%i):%i\n",*(s-1), *(t-1), *(s-1)==*(t-1), (int)t, (int)start_of_t, t>=start_of_t); while (*(--s) == *(--t) && t >= start_of_t) /* move backwards as long as t and s are identical, */ ;//printf("!%c:%c!",*s, *t); /* till you get to the beginning of t. */ //printf("\n %i==%i\n", (int)t, (int)start_of_t); if (t+1 == start_of_t) /* if we made it all the way through t; return true. */ return 1; return 0; } int main(void) { printf("(normal success) 1 == %i\n",mystrend("abcdef", "def")); printf("(empty t) 1 == %i\n",mystrend("abcdef", "")); printf("(last char wrong) 0 == %i\n",mystrend("abcdeg", "def")); printf("(first char wrong) 0 == %i\n",mystrend("abcqef", "def")); printf("(empty s) 0 == %i\n",mystrend("", "def")); printf("(both empty) 1 == %i\n",mystrend("", "")); }
Added a new solution without writing it in the summary subject
I would thank suggestions. I'm learning.
Greetings,
Jose G. López