From ea3c92d58cdf2d4030598d1052c2eb6cf2d8cc7e Mon Sep 17 00:00:00 2001 From: zlg Date: Tue, 8 Dec 2015 03:10:43 -0800 Subject: 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... --- ch7/7-01_upper-lower.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ch7/7-01_upper-lower.c (limited to 'ch7/7-01_upper-lower.c') 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 +#include +#include + +/* 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; +} -- cgit v1.2.3-54-g00ecf