lolamontes69

K+R Exercise5_4

Sep 7th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int strlen(char *s);
  4. int strend(char s[], char t[]);
  5.  
  6. int main()
  7. {
  8.     int i;
  9.    
  10.     char s[300]="cute froggy";
  11.     char t[]="froggy";
  12.     i = strend(s, t);
  13.     if(i == 1)
  14.         printf("'%s' appears at the end of '%s'",t,s);
  15.     else
  16.         printf("'%s' doesnt appear at the end of '%s'",t,s);
  17.     puts(" ");
  18.     return(0);
  19. }
  20.  
  21. /* strlen: return length of the char array that '*s' points to */
  22. int strlen(char *s)
  23. {
  24.     char *p = s;
  25.  
  26.     while(*p != '\0')
  27.         p++;
  28.     return p-s;
  29. }
  30.  
  31. /* strcmp: compare the arrays that *s and *t point to
  32.  * return 1 if they match and 0 if they don't */
  33. int strcmp(char *s, char *t)
  34. {
  35.     int i;
  36.  
  37.     for( ; *s == *t; s++, t++)
  38.         if(*s =='\0')
  39.             return 1;
  40.     return 0;
  41. }
  42.  
  43. /* strend: return 1 if "char t" is at the end of "char s" 0 if not */
  44. int strend(char s[], char t[])
  45. {
  46.     int i, j;
  47.    
  48.     i = strlen(&s[0]);
  49.     j = strlen(&t[0]);
  50.     i = i-j;
  51.     j = strcmp(&s[i],&t[0]);
  52.     return j;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment