aboutsummaryrefslogtreecommitdiff
path: root/1-22_wordwrap.c
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2013-01-09 01:10:15 -0600
committerzlg <zlg@zlg.space>2013-01-09 01:10:15 -0600
commit1f7068ac3281c0651c876d99612432a36c298f90 (patch)
tree34f1f5347390ccc04a052470c27909d50687e707 /1-22_wordwrap.c
parentClean up 1-13 and 1-20, correct 1-21 (diff)
downloadknr-1f7068ac3281c0651c876d99612432a36c298f90.tar.gz
knr-1f7068ac3281c0651c876d99612432a36c298f90.tar.bz2
knr-1f7068ac3281c0651c876d99612432a36c298f90.tar.xz
knr-1f7068ac3281c0651c876d99612432a36c298f90.zip
Solve Exercise 1-22: wordwrap
* Removed unnecessary get_line() function in 1-17 * Corrected grammar in README, added an acknowledgement
Diffstat (limited to '')
-rw-r--r--1-22_wordwrap.c75
1 files changed, 75 insertions, 0 deletions
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);
+ }
+
+}