summaryrefslogtreecommitdiff
path: root/ch1
diff options
context:
space:
mode:
Diffstat (limited to 'ch1')
-rw-r--r--ch1/char_hist.c30
-rw-r--r--ch1/copier.c29
-rw-r--r--ch1/counter.c29
-rw-r--r--ch1/ctof.c36
-rw-r--r--ch1/ftoc.c37
-rw-r--r--ch1/hello.c8
-rw-r--r--ch1/line_counter.c16
-rw-r--r--ch1/longest_line.c58
-rw-r--r--ch1/no_trailers.c72
-rw-r--r--ch1/num_digits.c29
-rw-r--r--ch1/one_word.c16
-rw-r--r--ch1/power.c28
-rw-r--r--ch1/reverse.c70
-rw-r--r--ch1/too_long.c39
-rw-r--r--ch1/wc.c29
-rw-r--r--ch1/whitespace_converter.c18
-rw-r--r--ch1/whitespace_counter.c22
-rw-r--r--ch1/whitespace_translator.c26
-rw-r--r--ch1/word_hist.c24
19 files changed, 616 insertions, 0 deletions
diff --git a/ch1/char_hist.c b/ch1/char_hist.c
new file mode 100644
index 0000000..312df76
--- /dev/null
+++ b/ch1/char_hist.c
@@ -0,0 +1,30 @@
+#include<stdio.h>
+
+#define CHARSET_LENGTH 128;
+
+/*
+ * Prints a histogram of the lengths of words in its input.
+ */
+main() {
+ int c, i;
+ int count;
+ int length = CHARSET_LENGTH;
+ int c_counts[length];
+
+ for (i = 0; i < length; ++i) {
+ c_counts[i] = 0;
+ }
+
+ while ((c = getchar()) != EOF) {
+ ++c_counts[c];
+ }
+
+ for (i = 0; i < length; ++i) {
+ count = c_counts[i];
+
+ if (count > 0 && (i != ' ' && i != '\t' && i != '\n' )) {
+ printf("%c\t", i);
+ printf("%d\n", count);
+ }
+ }
+}
diff --git a/ch1/copier.c b/ch1/copier.c
new file mode 100644
index 0000000..eeade8e
--- /dev/null
+++ b/ch1/copier.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+
+/* Copy input to output; 1st version */
+//main() {
+// int c;
+//
+// c = getchar();
+//
+// while (c != EOF) {
+// putchar(c);
+// c = getchar();
+// }
+//}
+
+/* Copy input to output; 2nd version */
+main() {
+ int c;
+
+ while ((c = getchar()) != EOF) {
+ putchar(c);
+ }
+}
+
+//main() {
+// printf("%d", EOF);
+// getchar();
+// printf("%d", getchar());
+// printf("%d", getchar());
+//}
diff --git a/ch1/counter.c b/ch1/counter.c
new file mode 100644
index 0000000..a1b9927
--- /dev/null
+++ b/ch1/counter.c
@@ -0,0 +1,29 @@
+#include<stdio.h>
+
+/*
+ * Count characters in input; 1st version
+ */
+//main() {
+// long nc;
+//
+// nc = 0;
+//
+// while (getchar() != EOF)
+// ++nc;
+//
+//
+// printf("%ld\n", nc);
+//}
+
+/*
+ * Count characters in input; 2nd version
+ */
+main() {
+ long nc;
+
+
+ for (nc = 0; getchar() != EOF; ++nc)
+ ;
+
+ printf("%ld\n", nc);
+}
diff --git a/ch1/ctof.c b/ch1/ctof.c
new file mode 100644
index 0000000..b95e20a
--- /dev/null
+++ b/ch1/ctof.c
@@ -0,0 +1,36 @@
+#include<stdio.h>
+
+#define LOWER 0;
+#define UPPER 300;
+#define STEP 20;
+
+float ctof(int c);
+
+/*
+ * Print Celcius-Fahrenheit table for celcius = 0, 20, ... 300.
+ */
+main() {
+ float fahr, celcius;
+ int lower, upper, step;
+
+ lower = LOWER; // lower limit of temperature table
+ upper = UPPER; // upper limit of temperature table
+ step = STEP; // step size
+
+ celcius = lower;
+
+ printf("%3s %6s\n", "C", "F");
+
+ while (celcius <= upper) {
+ fahr = ctof(celcius);
+ printf("%3.0f %6.1f\n", celcius, fahr);
+ celcius = celcius + step;
+ }
+}
+
+/*
+ * Convert Celcius to Fahrenheit
+ */
+float ctof(int celcius) {
+ return (9.0 / 5.0) * celcius + 32.0;
+}
diff --git a/ch1/ftoc.c b/ch1/ftoc.c
new file mode 100644
index 0000000..70b994a
--- /dev/null
+++ b/ch1/ftoc.c
@@ -0,0 +1,37 @@
+#include<stdio.h>
+
+#define LOWER 0 // lower limit of table
+#define UPPER 300 // upper limit of table
+#define STEP 20 // step size
+
+float ftoc(int fahr);
+
+/*
+ * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300.
+ */
+main() {
+ float fahr, celcius;
+ int lower, upper, step;
+
+ lower = LOWER; // lower limit of temperature table
+ upper = UPPER; // upper limit of temperature table
+ step = STEP; // step size
+
+ fahr = lower;
+
+ printf("%3s %6s\n", "F", "C");
+
+ // while (fahr <= upper) {
+ // celcius = 5.0 / 9.0 * (fahr - 32.0);
+ // printf("%3.0f %6.1f\n", fahr, celcius);
+ // fahr = fahr + step;
+ // }
+
+ for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) {
+ printf("%3.0f %6.1f\n", fahr, ftoc(fahr));
+ }
+}
+
+float ftoc(int fahr) {
+ return (5.0 / 9.0) * (fahr - 32);
+}
diff --git a/ch1/hello.c b/ch1/hello.c
new file mode 100644
index 0000000..b1ad928
--- /dev/null
+++ b/ch1/hello.c
@@ -0,0 +1,8 @@
+#include<stdio.h>
+
+main() {
+ printf("hello, ");
+ printf("world.");
+ printf("\n");
+}
+
diff --git a/ch1/line_counter.c b/ch1/line_counter.c
new file mode 100644
index 0000000..0447aa7
--- /dev/null
+++ b/ch1/line_counter.c
@@ -0,0 +1,16 @@
+#include<stdio.h>
+
+/*
+ * Count lines in input
+ */
+main() {
+ int c, nl;
+
+ nl = 0;
+
+ while ((c = getchar()) != EOF)
+ if (c == '\n')
+ ++nl;
+
+ printf("%d\n", nl);
+}
diff --git a/ch1/longest_line.c b/ch1/longest_line.c
new file mode 100644
index 0000000..58f2a72
--- /dev/null
+++ b/ch1/longest_line.c
@@ -0,0 +1,58 @@
+#include<stdio.h>
+#define MAXLENGTH 1000
+
+int get_line(char line[], int maxlength);
+void copy(char to[], char from[]);
+
+/*
+ * MAIN
+ * Prints longest line in STDIN.
+ */
+main() {
+ int len; // current line length
+ int max; // maximum length seen so far
+ char line[MAXLENGTH]; // current input line
+ char longest[MAXLENGTH]; // longest line saved here
+
+ max = 0;
+ while ((len = get_line(line, MAXLENGTH)) > 0)
+ if (len > max) {
+ max = len;
+ copy(longest, line);
+ }
+ if (max > 0) // there was a line
+ printf("%d\t", max);
+ printf("%s", longest);
+ return 0;
+}
+
+/*
+ * GET_LINE
+ * Read STDIN into LINE up to MAXLENGTH and return its length.
+ * Returns the length of a the line.
+ */
+int get_line(char line[], int maxlength) {
+ int c, i;
+
+ for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
+ line[i] = c;
+ if (c == '\n') {
+ line[i] = c;
+ ++i;
+ }
+ line[i] = '\0';
+ return i;
+}
+
+/*
+ * COPY
+ * Copy FROM into TO; assume TO is big enough.
+ */
+void copy(char to[], char from[]) {
+ int i;
+
+ i = 0;
+ while ((to[i] = from[i]) != '\0')
+ ++i;
+}
+
diff --git a/ch1/no_trailers.c b/ch1/no_trailers.c
new file mode 100644
index 0000000..80dbc0f
--- /dev/null
+++ b/ch1/no_trailers.c
@@ -0,0 +1,72 @@
+#include<stdio.h>
+
+#define MAXLENGTH 1000
+
+int get_line(char line[], int maxlength);
+void strip_line(char line[], int length);
+void copy(char to[], char from[]);
+
+/*
+ * MAIN
+ * Removes all trailing whitespace and blank lines from STDIN.
+ */
+int main() {
+ int len; // current line length
+ char line[MAXLENGTH]; // current input line
+
+ while ((len = get_line(line, MAXLENGTH)) > 0) {
+ strip_line(line, len);
+ if (line[0] != '\0')
+ printf("%s\n", line);
+ }
+ return 0;
+}
+
+/*
+ * GET_LINE
+ * Read STDIN into LINE up to MAXLENGTH and return its length.
+ * Returns the length of a the line.
+ */
+int get_line(char line[], int maxlength) {
+ int c, i;
+
+ for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
+ line[i] = c;
+ if (c == '\n') {
+ line[i] = c;
+ ++i;
+ }
+ line[i] = '\0';
+ return i;
+}
+
+/*
+ * STRIP_LINE
+ * Remove all whitespace from end of LINE of given LENGTH.
+ */
+void strip_line(char line[], int length) {
+ int i, c;
+
+ for (i = --length; i >= 0; --i) {
+ c = line[i];
+
+ if (c == ' ' || c == '\t' || c == '\n')
+ line[i] = '\0';
+ else
+ break;
+ }
+}
+
+
+/*
+ * COPY
+ * Copy FROM into TO; assume TO is big enough.
+ */
+void copy(char to[], char from[]) {
+ int i;
+
+ i = 0;
+ while ((to[i] = from[i]) != '\0')
+ ++i;
+}
+
diff --git a/ch1/num_digits.c b/ch1/num_digits.c
new file mode 100644
index 0000000..c25fc29
--- /dev/null
+++ b/ch1/num_digits.c
@@ -0,0 +1,29 @@
+#include<stdio.h>
+
+/*
+ * Count digits, whitespace, and other characters.
+ */
+main() {
+ int c, i, nwhite, nother;
+ int ndigit[10];
+
+ nwhite = nother = 0;
+
+ for (i = 0; i < 10; ++i)
+ ndigit[i] = 0;
+
+ while ((c = getchar()) != EOF)
+ if (c >= '0' && c <= '9')
+ ++ndigit[c - '0'];
+ else if (c == ' ' || c == '\n' || c == '\t')
+ ++nwhite;
+ else
+ ++nother;
+
+ printf("digits =");
+
+ for (i = 0; i < 10; ++i)
+ printf(" %d", ndigit[i]);
+ printf(", whitespace = %d, other = %d\n",
+ nwhite, nother);
+}
diff --git a/ch1/one_word.c b/ch1/one_word.c
new file mode 100644
index 0000000..8d50542
--- /dev/null
+++ b/ch1/one_word.c
@@ -0,0 +1,16 @@
+#include<stdio.h>
+
+/*
+ * Print input one word per line.
+ */
+main() {
+ int c;
+
+ while ((c = getchar()) != EOF) {
+ if (c == ' ' || c == '\n' || c == '\t')
+ putchar('\n');
+ else {
+ putchar(c);
+ }
+ }
+}
diff --git a/ch1/power.c b/ch1/power.c
new file mode 100644
index 0000000..8bca4fd
--- /dev/null
+++ b/ch1/power.c
@@ -0,0 +1,28 @@
+#include<stdio.h>
+
+int power(int m, int n);
+
+/*
+ * Test power function
+ */
+main() {
+ int i;
+
+ for (i = 0; i < 10; ++i)
+ printf("%d %d %d\n", i, power(2, i), power(-3, i));
+
+ return 0;
+}
+
+/*
+ * Raise base to n-th power; n >= 0
+ */
+int power(int base, int n) {
+ int i, p;
+
+ p = 1;
+
+ for (i = 1; i <= n; ++i)
+ p = p * base;
+ return p;
+}
diff --git a/ch1/reverse.c b/ch1/reverse.c
new file mode 100644
index 0000000..98edd98
--- /dev/null
+++ b/ch1/reverse.c
@@ -0,0 +1,70 @@
+#include<stdio.h>
+
+#define MAXLENGTH 1000
+
+// Prototypes
+int get_line(char line[], int maxlength);
+void line_copy(char to[], char from[]);
+void reverse_copy(char to[], char from[], int length);
+
+/*
+ * MAIN
+ * Reverse all lines on STDIN.
+ */
+int main() {
+ int len; // current line length
+ char line[MAXLENGTH]; // current input line
+ char reversed_line[MAXLENGTH]; // current reversed line
+
+ while ((len = get_line(line, MAXLENGTH)) > 0) {
+ reverse_copy(reversed_line, line, len);
+ printf("%s\n", reversed_line);
+ }
+ return 0;
+}
+
+/*
+ * GET_LINE
+ * Read STDIN into LINE up to MAXLENGTH.
+ * Returns the length of LINE.
+ */
+int get_line(char line[], int maxlength) {
+ int c, i;
+
+ for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
+ line[i] = c;
+ if (c == '\n') {
+ line[i] = c;
+ ++i;
+ }
+ line[i] = '\0';
+ return i;
+}
+
+/*
+ * LINE_COPY
+ * Copy FROM into TO; assume TO is big enough.
+ */
+void line_copy(char to[], char from[]) {
+ int i;
+
+ i = 0;
+ while ((to[i] = from[i]) != '\0')
+ ++i;
+}
+
+/*
+ * REVERSE_COPY
+ * Copy FROM into TO (of length LENGTH) in reverse order; assume TO is big
+ * enough.
+ */
+void reverse_copy(char to[], char from[], int length) {
+ int i;
+
+ for (i = 0; i < length; ++i) {
+ to[length - 1 - i] = from[i];
+ }
+
+ to[length] = '\0';
+}
+
diff --git a/ch1/too_long.c b/ch1/too_long.c
new file mode 100644
index 0000000..dcc6ed4
--- /dev/null
+++ b/ch1/too_long.c
@@ -0,0 +1,39 @@
+#include<stdio.h>
+
+#define MAXLENGTH 1000
+
+int get_line(char line[], int maxlength);
+void copy(char to[], char from[]);
+
+/*
+ * MAIN
+ * Print all lines larger than 80 characters.
+ */
+main() {
+ int len; // current line length
+ char line[MAXLENGTH]; // current input line
+
+ while ((len = get_line(line, MAXLENGTH)) > 0)
+ if (len > 80) {
+ printf("%s", line);
+ }
+ return 0;
+}
+
+/*
+ * GET_LINE
+ * Read STDIN into LINE up to MAXLENGTH and return its length.
+ * Returns the length of a the line.
+ */
+int get_line(char line[], int maxlength) {
+ int c, i;
+
+ for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
+ line[i] = c;
+ if (c == '\n') {
+ line[i] = c;
+ ++i;
+ }
+ line[i] = '\0';
+ return i;
+}
diff --git a/ch1/wc.c b/ch1/wc.c
new file mode 100644
index 0000000..4a5007d
--- /dev/null
+++ b/ch1/wc.c
@@ -0,0 +1,29 @@
+#include<stdio.h>
+
+#define IN 1 // inside a word
+#define OUT 0 // outside a word
+
+/*
+ * Count lines, words, and characters in input.
+ */
+main() {
+ int c, nl, nw, nc, state;
+
+ state = OUT;
+ nl = nw = nc = 0;
+
+ while ((c = getchar()) != EOF) {
+ ++nc;
+
+ if (c == '\n')
+ ++nl;
+ if (c == ' ' || c == '\n' || c == '\t')
+ state = OUT;
+ else if (state == OUT) {
+ state = IN;
+ ++nw;
+ }
+ }
+
+ printf("%d %d %d\n", nl, nw, nc);
+}
diff --git a/ch1/whitespace_converter.c b/ch1/whitespace_converter.c
new file mode 100644
index 0000000..6fee3bd
--- /dev/null
+++ b/ch1/whitespace_converter.c
@@ -0,0 +1,18 @@
+#include<stdio.h>
+
+/*
+ * Convert concurrent blanks to single blank.
+ */
+main() {
+ int c;
+
+ while ((c = getchar()) != EOF) {
+ if (c == ' ') {
+ while ((c = getchar()) == ' ')
+ ;
+ putchar(' ');
+ }
+
+ putchar(c);
+ }
+}
diff --git a/ch1/whitespace_counter.c b/ch1/whitespace_counter.c
new file mode 100644
index 0000000..ea441e2
--- /dev/null
+++ b/ch1/whitespace_counter.c
@@ -0,0 +1,22 @@
+#include<stdio.h>
+
+/*
+ * Count whitespace in input
+ */
+main() {
+ int c, n_whitespace;
+
+ n_whitespace = 0;
+
+ while ((c = getchar()) != EOF) {
+ if (c == '\n')
+ ++n_whitespace;
+ if (c == '\t')
+ ++n_whitespace;
+ if (c == ' ')
+ ++n_whitespace;
+ }
+
+
+ printf("%d\n", n_whitespace);
+}
diff --git a/ch1/whitespace_translator.c b/ch1/whitespace_translator.c
new file mode 100644
index 0000000..efb52b2
--- /dev/null
+++ b/ch1/whitespace_translator.c
@@ -0,0 +1,26 @@
+#include<stdio.h>
+
+/*
+ * Translate invisible whitespace characters to visible
+ * representations.
+ */
+main() {
+ int c;
+
+ while ((c = getchar()) != EOF) {
+ if (c == 8) {
+ printf("\\b");
+ continue;
+ }
+ if (c == 9) {
+ printf("\\t");
+ continue;
+ }
+ if (c == '\\') {
+ printf("\\\\");
+ continue;
+ }
+
+ putchar(c);
+ }
+}
diff --git a/ch1/word_hist.c b/ch1/word_hist.c
new file mode 100644
index 0000000..965c1b9
--- /dev/null
+++ b/ch1/word_hist.c
@@ -0,0 +1,24 @@
+#include<stdio.h>
+
+/*
+ * Prints a histogram of the lengths of words in its input.
+ */
+main() {
+ int c;
+ int count;
+
+ while ((c = getchar()) != EOF) {
+ if (c == '\n' || c == '\t' || c == ' ') {
+
+ while (count > 0) {
+ printf("%s", "*");
+ --count;
+ }
+
+ if (count > 0) {
+ printf("\n");
+ }
+ } else
+ ++count;
+ }
+}