Chapter 5

Exercise 5-4

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int mystrend(char *, char *);
 5     char s[100] = "ba";
 6     char *t = "ba";
 7 
 8     printf("%d\n", mystrend(s, t));
 9 }
10 
11 int mystrend(char *s, char *t)
12 {
13     char *bs = s;
14     char *bt = t;
15 
16     while (*s)
17         ++s;
18     while (*t)
19         ++t;
20 
21     if (t-bt > s-bs) // 如果串t比串s长,则应返回0
22         return 0;
23 
24     while (t >= bt && s >= bs)
25         if (*t-- != *s--)
26             return 0;
27     return 1;
28 }
29 
30 //int mystrend(char *s, char *t)
31 //{
32 //    char *bs = s; /*remember beginning of str s */
33 //    char *bt = t;
34 //    for ( ; *s; s++)
35 //        ;  /* end of the string s */
36 //    for (; *t; t++)  /* end of the string t */
37 //        ;
38 //    for (; *s == *t; s--,t--)
39 //        if(t == bt || s == bs)
40 //            break;  /* at the beginning of a str */
41 //    if (*s == *t && t == bt && *s != '\0')
42 //        return 1;
43 //    else
44 //        return 0;
45 //}

 

posted on 2019-09-20 10:22  李万三  阅读(71)  评论(0)    收藏  举报

导航