forked from wuqingze/cprogramminglanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.strend.c
105 lines (86 loc) · 1.78 KB
/
16.strend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
void test00();
void test01();
int _strend(char *s, char *t);
int __strend(char *s, char *t);
int main()
{
//printf("%d\n",__strend("hello", "hello"));
printf("%d\n",__strend("world", "hello"));
printf("%d\n",__strend("world", "ld"));
printf("%d\n",__strend("world", ""));
printf("%d\n",__strend("", ""));
printf("%d\n",__strend("", "ld"));
printf("%d\n",__strend(NULL, "ld"));
printf("%d\n",__strend("", NULL));
// test01();
return 0;
}
void test00(){
int strend(char*, char*);
// char *s = "qweasdzxc123Hello";
// char *t = "Hello";
char *s = "ed";
char *t = "dd";
int end = strend(s, t);
printf("%d\n", end);
}
void test01(){
int strend(char*, char*);
//char *s = "qweasdzxc123Hello";
// char *t = "Hello";
char *s = "ed";
char *t = "dd";
int end = _strend(s, t);
printf("%d\n", end);
}
int _strend(char *s, char *t){
if(NULL == s || NULL == t)
return 0;
int n =0;
int m =0;
while(*(s+n))
n++;
while(*(t+m))
m++;
printf("n=%d, m=%d\n", n, m);
if(n<m)
return 0;
for(int i=0;i<m;i++){
printf("t+i=%c, s+m-n+i=%c, i=%d\n", *(t+i), *(s+n-m+i), i);
if(*(t+i) != *(s+n-m+i))
return 0;
}
return 1;
}
int strend(char *s, char *t)
{
char *sStart = s;
char *tStart = t;
while (*s)
s++;
while (*t)
t++;
while ((*s-- == *t--) && (t != tStart) & (s != sStart))
;
if (t == tStart)
return 1;
else
return 0;
}
int __strend(char *s, char *t){
if(NULL == s || NULL == t)
return 0;
char *pres = s-1;
char *pret = t-1;
while(*s)
s ++;
while(*t)
t ++;
while( s!= pres && t!=pret && *s-- == *t--)
;
if(pret == t)
return 1;
else
return 0;
}