summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-05-14 15:23:53 -0400
committerAdam Carpenter <53hornet@gmail.com>2019-05-14 15:23:53 -0400
commit3c03bde0755595a104ceb2430e6842fd46de4e74 (patch)
tree954f4f7cda7d3c5587ab758f09b2300cb61ac73c
parent9706f875156e5ce4c08444489b634c54f4d62b52 (diff)
downloadlearning-c-3c03bde0755595a104ceb2430e6842fd46de4e74.tar.xz
learning-c-3c03bde0755595a104ceb2430e6842fd46de4e74.zip
Finished too_long, no_trailers
-rw-r--r--no_trailers.c72
-rw-r--r--too_long.c39
2 files changed, 111 insertions, 0 deletions
diff --git a/no_trailers.c b/no_trailers.c
new file mode 100644
index 0000000..e600e35
--- /dev/null
+++ b/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
+ * Prints longest line in 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/too_long.c b/too_long.c
new file mode 100644
index 0000000..dcc6ed4
--- /dev/null
+++ b/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;
+}