From 5d4b543ae9fbdaf7b86cbbd2b652808ce282c91f Mon Sep 17 00:00:00 2001 From: zlg Date: Mon, 2 Sep 2013 07:11:07 -0500 Subject: Solve Exercise 5-4: strend() Detailed answers below the question will only occur now if I cannot explain myself well enough in code and nearby comments. I'm looking to learn how to write better comments so there's less need for prose. --- ch5/5-04_strend.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ch5/5-04_strend.c (limited to 'ch5') diff --git a/ch5/5-04_strend.c b/ch5/5-04_strend.c new file mode 100644 index 0000000..8fec2a2 --- /dev/null +++ b/ch5/5-04_strend.c @@ -0,0 +1,37 @@ +#include +#include + +/* The C Programming Language: 2nd Edition + * + * Exercise 5-4: Write the function strend(s,t), which returns 1 if the string + * t occurs at the end of the string s, and zero otherwise. + * + * Answer: Check from the end of the string! + */ + +int strend(char *, char *); + +int main() { + char *foo = "kekt"; + char *bar = "buckets of kekt"; + printf("%d\n", strend(bar, foo)); // should output 1 + foo = "missile"; + bar = "Did you see those missiles?!"; + printf("%d\n", strend(bar, foo)); // should output 0 + return 0; +} + +int strend(char *s, char *t) { + // Go to the end of s + while (*s != '\0') { + s++; + } + // back up s by however long t is + s -= strlen(t); + // Check to be sure the rest of the string matches + for (; *s == *t && *s != '\0' && *t != '\0'; s++, t++); + if (*s == '\0' && *t == '\0') { + return 1; + } + return 0; +} -- cgit v1.2.3-70-g09d2 mmit/src/vgstash/test_vgstash_cli.py?h=next&id=7288560c5678991527c7b2c2ed0d7f3979f70c67&follow=1'>commitdiff
AgeCommit message (Expand)AuthorFilesLines
2018-09-29cli: Add pretty printing to 'list' commandzlg1-1/+49
2018-09-08setup.py: Bump to alpha4 for PyPIzlg1-1/+1
2018-09-08cli: add '--raw' option to list commandzlg2-9/+45
2018-09-08Add remaining filters to vgstash packagezlg1-2/+11
2018-09-04Update LICENSE to match setup.pyzlg1-80/+67
2018-09-03Branch off from master with pytest, tox, clickzlg16-778/+779
2018-03-18Flesh out filter types and ownership statuszlg3-82/+144
2018-03-18README.mdown: break line correctlyzlg1-1/+1
2018-03-18add 'playlog' list filterzlg2-2/+9
2018-03-13Update helpers a bitzlg1-2/+9
2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20
2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0
2016-11-15Correct phrasing in README.Ze Libertine Gamer1-4/+4
2016-11-13DerpZe Libertine Gamer1-0/+1
2016-11-03Improve error handling in shell scriptsZe Libertine Gamer4-3/+23
2016-10-24Correct run_again, add recursionZe Libertine Gamer1-0/+4
2016-10-21Add quotes to correct behavior for arglistZe Libertine Gamer1-1/+1
2016-10-14updater.sh: add recursion, error handlingZe Libertine Gamer1-43/+101
2016-10-14Correct pipe-handling behaviorZe Libertine Gamer1-1/+9
2016-10-12Clarify a method to move between platformsZe Libertine Gamer1-2/+5