aboutsummaryrefslogtreecommitdiff
path: root/ch2
diff options
context:
space:
mode:
Diffstat (limited to 'ch2')
-rw-r--r--ch2/2-04_squeeze-v2.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/ch2/2-04_squeeze-v2.c b/ch2/2-04_squeeze-v2.c
new file mode 100644
index 0000000..2c3b56a
--- /dev/null
+++ b/ch2/2-04_squeeze-v2.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+
+/* The C Programming Language: 2nd Edition
+ *
+ * Exercise 2-4: Write an alternate version of squeeze(s1, s2) that deletes
+ * each character in s1 that matches any character in the string s2.
+ *
+ * Answer: This one is fairly easy, considering the fun trick that's covered
+ * in the passage before this exercise with the unary operators ++ and --. C's
+ * interesting behavior with these operators allows the programmer to write
+ * shorter, faster logic.
+ *
+ * That said, I couldn't find a way to iterate through s2[] and s1[] in a
+ * single loop, and I needed to use a flag. There may be a more clever way to
+ * solve this.
+ *
+ */
+
+// It'd be better to make this return a pointer (or string), but the book
+// hasn't covered it yet!
+void squeeze(char s1[], char s2[]) {
+ int i, j, k, match;
+
+ for (i = j = 0; s1[i] != '\0'; i++) {
+ // I don't see a way to do this without a flag
+ match = 0;
+ for (k = 0; s2[k] != '\0'; k++) {
+ if (s1[i] == s2[k]) {
+ match = 1;
+ break;
+ }
+ }
+ // check our flag. if there wasn't a match, j needs to match i's value
+ if (!match) {
+ s1[j++] = s1[i];
+ }
+ }
+
+ s1[j] = '\0';
+}
+
+int main() {
+ char foo[16] = "foobarbaz";
+ char bar[16] = "boz";
+
+ squeeze(foo, bar);
+
+ printf("%s\n", foo); // Should read "fara"
+
+ return 0;
+}
2018-03-18README.mdown: break line correctlyzlg1-1/+1 2018-03-18add 'playlog' list filterzlg2-2/+9 This filter is used to get an idea of which games you're currently playing through, so you can prioritize games to play when you're bored and detect it when you've beaten a game but haven't marked it as such. 2018-03-13Update helpers a bitzlg1-2/+9 At present, user modification is needed to make these seamless. vgup() may need to be axed in favor of telling the user to make an alias. 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 It used to point to a directory, which would then look for .vgstash.db. This behavior was kind of backwards and I don't remember why I did it that way. This change gives users more control over where they put their DB. Be sure to update your environment variable if you have it set! 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 Sourcing them in .bash_profile screws up login if they're set. 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 Loops and functions -- oh my, what a useful combination. :) 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 Also correct a typo.