diff options
Diffstat (limited to '')
-rw-r--r-- | 1-17_over-80.c | 15 | ||||
-rw-r--r-- | 1-22_wordwrap.c | 75 | ||||
-rw-r--r-- | README.mdown | 13 |
3 files changed, 82 insertions, 21 deletions
diff --git a/1-17_over-80.c b/1-17_over-80.c index dcf7058..53f00ce 100644 --- a/1-17_over-80.c +++ b/1-17_over-80.c @@ -2,21 +2,6 @@ #define MINLENGTH 80 -/* Read as much as possible of a string and return its length. */ -int get_line(char s[]) { - int c, i; - - for (i = 0; i < MINLENGTH - 1 && (c = getchar()) != EOF; ++i) { - s[i] = c; - } - if (c == '\n') { - s[i] = c; - ++i; - } - s[i] = '\0'; - return i; -} - int main() { // longline is used as a boolean that tells us if it's a line worth printing int longline = 0; diff --git a/1-22_wordwrap.c b/1-22_wordwrap.c new file mode 100644 index 0000000..5e17b50 --- /dev/null +++ b/1-22_wordwrap.c @@ -0,0 +1,75 @@ +#include <stdio.h> + +/* The C Programming Language: 2nd Edition + * Exercise 1-22: + * "Write a program to 'fold' long input lines into two or more shorter lines + * after the last non-blank character that occurs before the n-th column of + * input. Make sure your program does something intelligent with very long + * lines, and if there are no blanks or tabs before the specified column." + * + * So... Quite a hefty requirement. In a nutshell, our goal is to create sane + * hard-wrapping. This is a common function in text editors, and it's + * important to get it right or the results are wonky. + * + * + * TODO: get_line() is not fully correct. When it hits a \t, it counts it as + * one character and has no concept of display count. I'll fix this later on. + */ + +// For tradition's sake, let's wrap at 80 columns +#define MAXLEN 80 + +char data[MAXLEN]; +int i, j, k; + +int get_line(char s[], int lim) { + /* Put as much as possible into a temp string, and count its length */ + int c, i; + + for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; ++i) { + s[i] = c; + } + if (c == '\n') { + s[i] = c; + ++i; + } + s[i] = '\0'; + return i; +} + +/* Find the first blank character, starting from the end of the string. Returns + * the position of the blank, or -1 if one wasn't found. + */ +int b_find_blank(char s[], int lim) { + // Start at the end of the string and go backwards. + for (i = lim; i >= 0; i--) { + // Simply replace the first blank with a newline. + if (s[i] == ' ' || s[i] == '\t') { + return i; + } + } + return -1; +} + +int main() { + while (j = get_line(data, MAXLEN)) { + if (j == 80) { + // We know it's a long line now. Let's make sure we're breaking in + // the right place + k = b_find_blank(data, MAXLEN); + //printf("%d\n", k); + if (k > -1) { + data[k] = '\n'; + data[MAXLEN] = '\0'; + printf("%s", data); + continue; + } else { + data[MAXLEN] = '\0'; + printf("%s\n", data); + continue; + } + } + printf("%s", data); + } + +} diff --git a/README.mdown b/README.mdown index 1fafb61..f5d961a 100644 --- a/README.mdown +++ b/README.mdown @@ -1,8 +1,8 @@ -These files are my solutions to Kernighan and Ritchie's *The C Programming -Language, 2nd Edition*. My aim is to deepen my understanding of C, using only -the concepts covered in content that comes before each exercise. That means no -"cheating" by using standard library features that aren't been covered in the -book yet. As a result, many of my solutions will probably not be "the best", but +These files are my solutions to Kernighan and Ritchie's *The C Programming +Language, 2nd Edition*. My aim is to deepen my understanding of C, using only +the concepts covered in content that comes before each exercise. That means no +"cheating" by using standard library features that aren't covered in the book +yet. As a result, many of my solutions will probably not be "the best", but suitable for the goal at hand: *learning*. I chose this route because as I was reading the K&R, I didn't feel like I was @@ -12,4 +12,5 @@ why was I learning C? I tackled a few exercises and found that I was beginning to learn a few things I hadn't ran into before. Critique is welcome, but please keep in mind the limitations I've outlined in -the first paragraph. +the first paragraph. Also note that some incomplete implementations are noted, +which I intend on revisiting. Any suggestions and advice are welcome. |