From ef44f44b21c88b1ae3e19348a5e5ef64d3ed2ed6 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Fri, 31 Dec 2021 16:34:34 +0800 Subject: 5.4 --- 5/4.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 5/4.c (limited to '5/4.c') 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 +#include +#include + +#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 -- cgit v1.2.3