5.4.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Write the function strend(s,t), which returns 1 if the string t occurs at the end of the string s, and zero otherwise. */
  2. #include <stdio.h>
  3. enum { FALSE, TRUE };
  4. int equal (a,b);
  5. int strend(s,t);
  6. int main(int argc, char** argv)
  7. {
  8. if (argc == 3) printf("%s %s",argv[1],argv[2]);
  9. return strend(argv[1],argv[2]);
  10. }
  11. int strend(char s[],char t[])
  12. {
  13. //figure out thow long t is;
  14. int sizes = 0;
  15. char *sptr = s;
  16. if (sptr == 0)
  17. return -1;
  18. while(*sptr)
  19. {
  20. sizes++;
  21. sptr++;
  22. }
  23. int sizet = 0;
  24. char *tptr = t;
  25. if (tptr == 0)
  26. return -1;
  27. while(*tptr)
  28. {
  29. sizet++;
  30. tptr++;
  31. }
  32. if (sizes < sizet) return 0;
  33. else
  34. {
  35. char *tptr2 = t;
  36. char *sptr2 = sptr-sizet; //off by 1?
  37. while ((*sptr2) && equal(sptr2,tptr2))
  38. {
  39. sptr2++;
  40. tptr2++;
  41. }
  42. return (*tptr2 == 0);
  43. }
  44. //figure out how long s is;
  45. //if t can't fit in s return 0;
  46. //if it *is* long enough, go back far enough and start checking
  47. //if the whole thing is there return 1
  48. }
  49. int equal (char* a, char* b)
  50. {
  51. if (*a == *b )
  52. return TRUE;
  53. else return FALSE;
  54. }