aboutsummaryrefslogtreecommitdiff
path: root/ch7
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2015-12-08 03:10:43 -0800
committerzlg <zlg@zlg.space>2015-12-08 03:10:43 -0800
commitea3c92d58cdf2d4030598d1052c2eb6cf2d8cc7e (patch)
tree252f0279f578665f1336ef311c57067bec903e7e /ch7
parentSolve Exercise 6-6: A simple `#define` processor (diff)
downloadknr-ea3c92d58cdf2d4030598d1052c2eb6cf2d8cc7e.tar.gz
knr-ea3c92d58cdf2d4030598d1052c2eb6cf2d8cc7e.tar.bz2
knr-ea3c92d58cdf2d4030598d1052c2eb6cf2d8cc7e.tar.xz
knr-ea3c92d58cdf2d4030598d1052c2eb6cf2d8cc7e.zip
Solve Exercise 7-1: `upper` and `lower`
Two cases, one file. This exercise was a welcome departure from the recent head-scratchers. It made me think about a solution for the pathname issue I realized would crop up, too. I'm roughly 75% done with the book! Maybe by the time I finish it, I'll have a Makefile ready for the project...
Diffstat (limited to 'ch7')
-rw-r--r--ch7/7-01_upper-lower.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/ch7/7-01_upper-lower.c b/ch7/7-01_upper-lower.c
new file mode 100644
index 0000000..2f641c1
--- /dev/null
+++ b/ch7/7-01_upper-lower.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+
+/* The C Programming Language: 2nd Edition
+ *
+ * Exercise 7-1: Write a program that converts upper case to lower or lower
+ * case to upper, depending on the name it is invoked with, as found in
+ * `argv[0]`.
+ *
+ * Notes: This one is, strangely, very easy. The primary gotcha is in the
+ * argv[0] check. The simplest solution I came up with was to check argv[0]
+ * for *both* upper and lower. If for some reason they're both in the path,
+ * the one that occurred later in the string takes precedence, since it's
+ * most likely to be the name of the executable itself rather than the
+ * directory containing the binary.
+ *
+ * Edge cases, gotta love'em.
+ */
+
+int main(int argc, char *argv[]) {
+ char c;
+ int lpos, upos;
+ char *plow, *pupp;
+ while ((c = getchar()) != EOF) {
+ plow = strstr(argv[0], "lower");
+ pupp = strstr(argv[0], "upper");
+ if (plow != NULL) {
+ lpos = (int) (plow - argv[0]);
+ } else {
+ lpos = -1;
+ }
+ if (pupp != NULL) {
+ upos = (int) (pupp - argv[0]);
+ } else {
+ upos = -1;
+ }
+ if (isalpha(c)) {
+ /* These two are only equal when neither are found in argv[0].
+ * The odds of this happening are slim, but still possible.
+ */
+ if (lpos > upos) {
+ c = tolower(c);
+ }
+ if (upos > lpos) {
+ c = toupper(c);
+ }
+ }
+ putchar(c);
+ }
+ return 0;
+}