aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--1-13_word-length-histogram.c36
-rw-r--r--1-20_detab.c5
-rw-r--r--1-21_entab.c43
-rw-r--r--README.mdown22
4 files changed, 60 insertions, 46 deletions
diff --git a/1-13_word-length-histogram.c b/1-13_word-length-histogram.c
index 1e21378..d3115a5 100644
--- a/1-13_word-length-histogram.c
+++ b/1-13_word-length-histogram.c
@@ -6,18 +6,17 @@
int main(void) {
/* Rundown of variables:
- c = current input char
- state = inside or outside a word
- ltrs = letter count
- wrds = word count
- lines = you should be shot if you don't know
- lengths = an array that keeps track of how often words up to x chars long
- occur.
- */
+ * i, j = reusable placeholder variables
+ * state = inside or outside a word
+ * ltrs = letter count
+ * wrds = word count
+ * lines = you should be shot if you don't know
+ * lengths = an array that keeps track of how often words up to x chars long
+ * occur.
+ */
- int c, state, ltrs, wrds, lines, wlen;
+ int state, ltrs, wrds, lines, wlen, i, j;
int lengths[MAXWLENGTH];
- int i;
for (i = 0; i <= MAXWLENGTH; ++i) {
lengths[i] = 0;
}
@@ -26,9 +25,9 @@ int main(void) {
lines = 1;
state = OUT;
// Capture input until it ends
- while ((c = getchar()) != EOF) {
+ while ((i = getchar()) != EOF) {
// If it's whitespace, we've exited a word
- if (c == '\n' || c == ' ' || c == '\t') {
+ if (i == '\n' || i == ' ' || i == '\t') {
if (state == IN) {
++wrds; // ...and should increase the count.
state = OUT;
@@ -41,7 +40,7 @@ int main(void) {
}
/* If it's a new line, we're still out of a word but need to increment the
line count */
- if (c == '\n') {
+ if (i == '\n') {
++lines;
}
} else {
@@ -59,19 +58,18 @@ int main(void) {
}
printf("\n"); // End the chart heading.
- int iter;
- iter = MINWLENGTH;
- while (iter <= MAXWLENGTH) {
- i = lengths[iter];
+ j = MINWLENGTH;
+ while (j <= MAXWLENGTH) {
+ i = lengths[j];
if (i > 0) {
- printf("%2d | ", iter);
+ printf("%2d | ", j);
while (i > 0) {
printf("#");
i = i-1;
}
printf("\n");
}
- ++iter;
+ ++j;
}
printf("%d words, %d chars, %d lines.\n", wrds, ltrs, lines);
return 0;
diff --git a/1-20_detab.c b/1-20_detab.c
index e1549e6..dc8f5b5 100644
--- a/1-20_detab.c
+++ b/1-20_detab.c
@@ -11,9 +11,8 @@
* worked with and it's not susceptible to scope. Though, in this simple
* program it really doesn't matter.
*
- * To be correct, this file should use the isprint() function to ensure that
- * 'column' only gets incremented when a printable character is being read. I'll
- * come back to this when I learn more of the stdlib.
+ * The "correct" solution uses the isprint() stdlib function, but it's not
+ * covered by this point in the book, so I did not use it.
*/
#define TABWIDTH 8
diff --git a/1-21_entab.c b/1-21_entab.c
index 0f25706..6037a14 100644
--- a/1-21_entab.c
+++ b/1-21_entab.c
@@ -15,30 +15,41 @@
#define TABWIDTH 8
int main(void) {
- int c, spaces;
- spaces = 0;
+ int column, c, spaces;
+ spaces = column = 0;
while ((c = getchar()) != EOF) {
- // Make sure the character is a space...
+ // First thing's first, advance by a column.
+ column++;
+
if (c == ' ') {
- ++spaces;
- // When spaces is equal to TABWIDTH, we can add a tab
- if (spaces == TABWIDTH) {
+ /* Add to 'spaces' immediately, we'll decide if it needs to be
+ * output later.
+ */
+ spaces++;
+
+ if (column % TABWIDTH == 0 && spaces > 0) {
putchar('\t');
- spaces = 0;
+ spaces = 0; // No spaces are left when we tab!
}
+
} else {
- /* As soon as we hit a non-space character, we need to make sure
- * there aren't 1-7 spaces leftover. These need to be output before
- * we output the non-space character itself! This little loop is
- * interesting because it solves the problem of leftover spaces
- * _and_ gets the 'spaces' back to zero, which it needs to be once
- * we hit a non-space character.
+ /* Be sure to output any leftover spaces when we come across a
+ * non-space character. This should allow for spaces between words
+ * that don't fall along the tabstop lines.
*/
- while (spaces != 0) {
+
+ while (spaces > 0) {
putchar(' ');
- --spaces;
+ spaces--;
}
- // Output the non-space character.
+
+ // As usual, reset things on a newline.
+ if (c == '\n') {
+ column = 0;
+ spaces = 0;
+ }
+
+ // Now we can output whatever it is.
putchar(c);
}
}
diff --git a/README.mdown b/README.mdown
index 7af39e0..1fafb61 100644
--- a/README.mdown
+++ b/README.mdown
@@ -1,9 +1,15 @@
-As I was reading Kernighan and Ritchie's *The C Programming Language*, I felt
-like I was learning the syntax and other superficial things, but not learning
-the more practical lessons that I was sure I'd run into once I tried to make
-something. So I chose to do the exercises. I didn't start until the middle of
-Chapter 1, but better late than never!
+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
+suitable for the goal at hand: *learning*.
-If you have suggestions or pointers, message me! I really want to learn C "the
-right way". It's a fascinating, spartan, pretty language, and I want to become
-good with it.
+I chose this route because as I was reading the K&R, I didn't feel like I was
+learning anything new, but I knew C was known for its simplicity and efficiency.
+If I wasn't learning anything about efficiency and simplifying my programs, then
+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.