summaryrefslogtreecommitdiff
path: root/ch01
diff options
context:
space:
mode:
authorZhineng Li <[email protected]>2026-02-04 10:49:18 +0800
committerZhineng Li <[email protected]>2026-02-04 10:49:18 +0800
commit6c133eab750473e3b0e1a342fafe55ba6f1a9c93 (patch)
tree9dac1c5798076220906071d3d611413d5ff6bd82 /ch01
parent300a343be85905883df27550145e4a687b91f691 (diff)
downloadc-knr-exercises-6c133eab750473e3b0e1a342fafe55ba6f1a9c93.tar.gz
c-knr-exercises-6c133eab750473e3b0e1a342fafe55ba6f1a9c93.zip
ch01-17: print lines that are longer than 80 characters
Diffstat (limited to 'ch01')
-rw-r--r--ch01/ex17-print.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/ch01/ex17-print.c b/ch01/ex17-print.c
new file mode 100644
index 0000000..35bb18a
--- /dev/null
+++ b/ch01/ex17-print.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#define MAXLINE 1000 /* maximum input line size */
+
+int getln(char line[], int maxline);
+
+/* print lines that are longer than 80 characters */
+int main()
+{
+ int len; /* current line length */
+ char line[MAXLINE]; /* current input line */
+
+ while ((len = getln(line, MAXLINE)) > 0)
+ if (len > 80)
+ printf("%s", line);
+ return 0;
+}
+
+/* getln: read a line into s, return length */
+int getln(char s[], int lim)
+{
+ int c, i;
+
+ for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
+ s[i] = c;
+ if (c == '\n') {
+ s[i] = c;
+ ++i;
+ }
+ s[i] = '\0';
+ return i;
+}