123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /* Write the function strend(s,t), which returns 1 if the string t occurs at the end of the string s, and zero otherwise. */
- #include <stdio.h>
- enum { FALSE, TRUE };
- int equal (a,b);
- int strend(s,t);
- int main(int argc, char** argv)
- {
- if (argc == 3) printf("%s %s",argv[1],argv[2]);
- return strend(argv[1],argv[2]);
- }
- int strend(char s[],char t[])
- {
- //figure out thow long t is;
- int sizes = 0;
- char *sptr = s;
- if (sptr == 0)
- return -1;
- while(*sptr)
- {
- sizes++;
- sptr++;
- }
- int sizet = 0;
- char *tptr = t;
- if (tptr == 0)
- return -1;
- while(*tptr)
- {
- sizet++;
- tptr++;
- }
- if (sizes < sizet) return 0;
- else
- {
- char *tptr2 = t;
- char *sptr2 = sptr-sizet; //off by 1?
- while ((*sptr2) && equal(sptr2,tptr2))
- {
- sptr2++;
- tptr2++;
- }
- return (*tptr2 == 0);
- }
-
- //figure out how long s is;
- //if t can't fit in s return 0;
- //if it *is* long enough, go back far enough and start checking
- //if the whole thing is there return 1
-
- }
- int equal (char* a, char* b)
- {
- if (*a == *b )
- return TRUE;
- else return FALSE;
- }
|