diff options
author | zlg <zlg@zlg.space> | 2013-09-26 07:17:51 -0500 |
---|---|---|
committer | zlg <zlg@zlg.space> | 2013-09-26 07:17:51 -0500 |
commit | 22ddf92e89b19c30ff77b368cab60cc95315db35 (patch) | |
tree | 5085347805262c7e35b8ce5393fd8b4787f7285a /ch5 | |
parent | Clean up 5-10's printf() calls (diff) | |
download | knr-22ddf92e89b19c30ff77b368cab60cc95315db35.tar.gz knr-22ddf92e89b19c30ff77b368cab60cc95315db35.tar.bz2 knr-22ddf92e89b19c30ff77b368cab60cc95315db35.tar.xz knr-22ddf92e89b19c30ff77b368cab60cc95315db35.zip |
Solve Exercise 5-11: `entab` and `detab` remixed!
This one was pretty rough. At first I wanted to include a bunch of
error-catching and "smart" stuff. When I took a second look at it and
realized `itoa` returns 0 on a string, things became a bit easier. I
may have been able to outsource a few things to a function or two, but
overall I think it worked out.
Diffstat (limited to '')
-rw-r--r-- | ch5/5-11_detab-remixed.c | 61 | ||||
-rw-r--r-- | ch5/5-11_entab-remixed.c | 76 |
2 files changed, 137 insertions, 0 deletions
diff --git a/ch5/5-11_detab-remixed.c b/ch5/5-11_detab-remixed.c new file mode 100644 index 0000000..43bffb4 --- /dev/null +++ b/ch5/5-11_detab-remixed.c @@ -0,0 +1,61 @@ +#include <stdio.h> + +/* The C Programming Language: 2nd Edition + * + * Exercise 5-11: Modify the programs entab and detab (written as exercises in + * Chapter 1) to accept a list of tab stops as arguments. Use the default tab + * settings if there are no arguments. + */ + +#define TABWIDTH 8 + +int main(int argc, char *argv[]) { + int column, c, tabnum, stop; + column = 0; + + if (argc > 1) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } else { + tabnum = 0; + } + + while ((c = getchar()) != EOF) { + if (c == '\t') { + if (argc > 1) { + // advance the argument if we're ahead of the last one + if (column > stop && tabnum < (argc -1)) { + stop = atoi(argv[++tabnum]); + } + // insert our spaces up to the tabstop + while (column <= stop) { + putchar(' '); + column++; + } + // advance the argument (again) if needed. + if (tabnum < (argc - 1)) { + stop = atoi(argv[++tabnum]); + } + } else { + // default tabstopping + while (column % TABWIDTH != 1) { + putchar(' '); + column++; + } + } + } else { + // reset counters and the arglist + if (c == '\n') { + column = 0; + if (tabnum > 0) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } + } + putchar(c); + column++; + } + } + + return 0; +} diff --git a/ch5/5-11_entab-remixed.c b/ch5/5-11_entab-remixed.c new file mode 100644 index 0000000..8f6f7d4 --- /dev/null +++ b/ch5/5-11_entab-remixed.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> + +/* The C Programming Language: 2nd Edition + * + * Exercise 5-11: Modify the programs entab and detab (written as exercises in + * Chapter 1) to accept a list of tab stops as arguments. Use the default tab + * settings if there are no arguments. + */ + +/* The directions for this exercise aren't terribly clear. The way I see it, + * each argument is a character column that all tabs should align to. For the + * entab portion, I should count 2+ spaces forward and, if I reach a tabstop, + * insert a tab. Then move onto the next argument. If 2+ spaces are present, + * but don't reach the tabstop threshold, the spaces are left alone. This is + * the best I could manage with the ambiguous instructions. + */ + +#define TABWIDTH 8 + +int main(int argc, char *argv[]) { + int column, c, spaces, tabnum, stop; + spaces = column = 0; + + if (argc > 1) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } else { + tabnum = 0; + } + + while ((c = getchar()) != EOF) { + column++; + + if (c == ' ') { + spaces++; + if (argc > 1) { + if (column == stop) { + if (tabnum < (argc - 1)) { + stop = atoi(argv[++tabnum]); + } + if (spaces > 1) { + putchar('\t'); + spaces = 0; + } + } else if (column > stop && tabnum < (argc -1)) { + stop = atoi(argv[++tabnum]); + } + } else { + // we need to do default tabstopping + if (column % TABWIDTH == 0 && spaces > 1) { + putchar('\t'); + spaces = 0; + } + } + } else { + // output all extra spaces first + while (spaces > 0) { + putchar(' '); + spaces--; + } + // reset the counts and argument position on a newline + if (c == '\n') { + column = 0; + spaces = 0; + if (tabnum > 0) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } + } + putchar(c); + } + } + + return 0; +} |