Added example with variable arrays.
[C-Programming-Examples.git] / ex_5-4.c
blob4da1d5cf9e4a7f002bd0de7eda3ef69de283a381
1 #include <stdio.h>
3 /* strcmp - compare strings */
4 /* return <0 is s < t; 0 if s==t; >0 if s>t */
5 int strcmp(char *s, char *t)
7 int i;
8 for(i = 0; s[i] == t[i]; i++)
11 return i; // might need to change
15 /* strlen - return length of string */
16 int strlen_new(char *s)
18 int n;
19 for(n =0; s[n] != '\0'; n++);
20 return n;
23 /* strend - compares end of string s with string t */
24 /* return 1 if string t occurs and 0 otherwise */
25 int strend(char *s, char *t)
27 int ls, lt;
28 ls = strlen_new(s); // find length of s
29 lt = strlen_new(t); // find length of t
31 // start with end of s and move back comparing with end of t
32 while(lt >= 0)
34 if(s[ls] != t[lt]) { return 0; }
35 --ls;
36 --lt;
38 return 1;
42 int main()
44 char s[] = { "1234567890" };
45 char t[] = { "67890" };
47 int ans = strend(s,t);
48 printf("%d\n", ans);
50 return 1;