summaryrefslogtreecommitdiffstats
path: root/5/4.c
diff options
context:
space:
mode:
Diffstat (limited to '5/4.c')
-rw-r--r--5/4.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/5/4.c b/5/4.c
new file mode 100644
index 0000000..adc15f0
--- /dev/null
+++ b/5/4.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAXLEN 1000
+
+/* Returns 1 if t occurs at the end of s, 0 otherwise */
+int strend(char *s, char *t);
+
+int main(int argc, char *argv[]) {
+ char *s, *t;
+
+ s = malloc(sizeof(char) * MAXLEN);
+ t = malloc(sizeof(char) * MAXLEN);
+
+ printf("first str: ");
+ fgets(s, MAXLEN, stdin);
+ s[strlen(s) - 1] = 0;
+
+ printf("second str: ");
+ fgets(t, MAXLEN, stdin);
+ t[strlen(t) - 1] = 0;
+
+ printf("rv: %s\n", strend(s, t) ? "true" : "false");
+
+ return 0;
+}
+
+int strend(char *s, char *t) {
+ size_t slen, tlen;
+
+ slen = strlen(s);
+ tlen = strlen(t);
+
+ if (slen < tlen)
+ return 0;
+
+ for (s += slen - tlen; *s != 0; s++, t++) {
+ if (*s != *t)
+ return 0;
+ }
+
+ return 1;
+} \ No newline at end of file