]> zdv2.bktei.com Git - BK-2023-05.git/commitdiff
feat(src/kr/ch1/s1.9):Finish exercise e1-16 (longest line)
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 14 Sep 2022 13:29:29 +0000 (13:29 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 14 Sep 2022 13:29:29 +0000 (13:29 +0000)
src/kr_exercises/ch1/s1.9/e1-16..longest_line.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.9/s1.9..longest_line.c [new file with mode: 0644]
src/notes.tm

diff --git a/src/kr_exercises/ch1/s1.9/e1-16..longest_line.c b/src/kr_exercises/ch1/s1.9/e1-16..longest_line.c
new file mode 100644 (file)
index 0000000..38b7258
--- /dev/null
@@ -0,0 +1,76 @@
+#include <stdio.h>
+
+/* Exercise 1-16. Revise the main routine of the longest-line program
+   so it will correctly print the length of arbitrarily long input
+   lines, and as much as possible of the text. */
+
+/* Strat: Modify mygetline() to read a char and increment `i` even if
+   `i<lim-1` is `FALSE`, until `EOF` or `\n` is encountered. However,
+   only store char to mygetline()'s output array if `i<lim-1` is TRUE;
+   otherwise, only increment `i`. Then, after `EOF` or `\n`
+   encountered, terminate line with `\n\0` so `\0` occupies last
+   element of `line[]` (i.e. 'line[MAXLINE-1]'). Return `i`.
+
+   Print `i` before contents of `line[]` array to communicate length
+   of string stored in `line[]`.
+*/
+
+#define MAXLINE 1000     /* maximum input line size */
+
+int mygetline(char line[], int maxline); /* 'my-' to avoid name collision */
+void copy(char to[], char from[]);
+
+/* print longest input line */
+int main() {
+  int len;            /* current line length */
+  int max;            /* maximum length seen so far */
+  char line[MAXLINE];    /* current input line */
+  char longest[MAXLINE]; /* longest line saved here */
+
+  max = 0;
+  while ((len = mygetline(line, MAXLINE)) > 0)
+    if (len > max) {
+      max = len;
+      copy(longest, line);
+    }
+  if (max > 0) /* there was a line */
+    printf("%03d:%s", max, longest);
+  return 0;
+};
+
+/* mygetline: read a line into s, return length v2 */
+int mygetline(char s[], int lim) {
+  int c, i;
+
+  for (i=0; (c=getchar())!=EOF && c!='\n'; ++i) {
+    if (i<lim-1)
+      /* Store char c to s if c not a newline or EOF */
+      s[i] = c;
+  };
+
+  if (i < lim-1) {
+    /* Case:lim will not be exceeded */
+    if (c == '\n') {
+      /* Handle c is '\n' */
+      s[i] = c;
+      ++i;
+    };
+    s[i] = '\0';
+  } else {
+    /* Case:lim will be exceeded */
+    /* Terminate s with '\n\0' */
+    s[lim-2] = '\n';
+    ++i;
+    s[lim-1] = '\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/src/kr_exercises/ch1/s1.9/s1.9..longest_line.c b/src/kr_exercises/ch1/s1.9/s1.9..longest_line.c
new file mode 100644 (file)
index 0000000..f947e28
--- /dev/null
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#define MAXLINE 1000     /* maximum input line size */
+
+int mygetline(char line[], int maxline); /* 'my-' to avoid name collision */
+void copy(char to[], char from[]);
+
+/* print longest input line */
+int main() {
+  int len;            /* current line length */
+  int max;            /* maximum length seen so far */
+  char line[MAXLINE];    /* current input line */
+  char longest[MAXLINE]; /* longest line saved here */
+
+  max = 0;
+  while ((len = mygetline(line, MAXLINE)) > 0)
+    if (len > max) {
+      max = len;
+      copy(longest, line);
+    }
+  if (max > 0) /* there was a line */
+    printf("%s", longest);
+  return 0;
+};
+
+/* mygetline: read a line into s, return length */
+int mygetline(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;
+}
+
+/* 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;
+}
index 37db3497ec28b2373c55f727731c2854cc2142f5..30d61d87532f58552a8d4eabbf0eea8336d972ae 100644 (file)
       <item*|<cpp|\\">>Represents the <em|double quote> character.
 
       <item*|<cpp|\\><em|ooo>>Represents an <em|octal number> (e.g.
-      <cpp|\\012> is the <em|newline> character<\footnote>
+      <cpp|\\012> is the <em|newline> character)<\footnote>
         <label|ref includehelp-oct-hex>See
         <hlinkv|https://www.includehelp.com/c/octal-and-hexadecimal-escape-sequences.aspx>.
-      </footnote>).
+      </footnote>.
 
       <item*|<cpp|\\x><em|hh>>Represents a <em|hexadecimal> number (e.g.
-      <cpp|\\x0A> is the <em|newline> character<rsup|<reference|ref
-      includehelp-oct-hex>>).
+      <cpp|\\x0A> is the <em|newline> character)<rsup|<reference|ref
+      includehelp-oct-hex>>.
+
+      <item*|<cpp|\\0>>Represents the <em|null> character (i.e. a <cpp|char>
+      with value <cpp|0>)<\footnote>
+        See <hlinkv|https://www.geeksforgeeks.org/difference-between-null-pointer-null-character-0-and-0-in-c-with-examples/>.
+      </footnote>
     </description-aligned>
 
     <label|term_expression><item*|expression> A sequence of
     <associate|auto-7|<tuple|2.2|?>>
     <associate|footnote-1.1.1|<tuple|1.1.1|?>>
     <associate|footnote-1.1.10|<tuple|1.1.10|?>>
+    <associate|footnote-1.1.11|<tuple|1.1.11|?>>
     <associate|footnote-1.1.2|<tuple|1.1.2|?>>
     <associate|footnote-1.1.3|<tuple|1.1.3|?>>
     <associate|footnote-1.1.4|<tuple|1.1.4|?>>
     <associate|footnote-1.2.5|<tuple|1.2.5|?>>
     <associate|footnr-1.1.1|<tuple|1.1.1|?>>
     <associate|footnr-1.1.10|<tuple|1.1.10|?>>
+    <associate|footnr-1.1.11|<tuple|1.1.11|?>>
     <associate|footnr-1.1.2|<tuple|1.1.2|?>>
     <associate|footnr-1.1.3|<tuple|1.1.3|?>>
     <associate|footnr-1.1.4|<tuple|1.1.4|?>>
     <associate|term definition|<tuple|1.1.1|?>>
     <associate|term enumeration|<tuple|linter|?>>
     <associate|term escape_sequence|<tuple|enumeration|?>>
-    <associate|term garbage_collection|<tuple|1.1.9|?>>
+    <associate|term garbage_collection|<tuple|1.1.10|?>>
     <associate|term heap|<tuple|garbage collection|?>>
     <associate|term machine-independent|<tuple|<label|term heap>heap|?>>
     <associate|term operand|<tuple|<label|term
     machine-independent>machine-independent|?>>
     <associate|term operator|<tuple|<label|term operand>operand|?>>
     <associate|term stack|<tuple|parameter|?>>
-    <associate|term struct|<tuple|1.1.10|?>>
+    <associate|term struct|<tuple|1.1.11|?>>
     <associate|term structure_assignment|<tuple|<label|term struct>struct|?>>
     <associate|term text_stream|<tuple|structure assignment|?>>
     <associate|term_argument|<tuple|1.1|?>>
     <associate|term_call|<tuple|assignment|?>>
-    <associate|term_delinter|<tuple|1.1.2|?>>
-    <associate|term_expression|<tuple|<with|mode|<quote|prog>|prog-language|<quote|cpp>|font-family|<quote|rm>|\\x><with|font-shape|<quote|italic>|hh>|?>>
-    <associate|term_function|<tuple|1.1.7|?>>
+    <associate|term_expression|<tuple|1.1.4|?>>
+    <associate|term_function|<tuple|1.1.8|?>>
     <associate|term_function_call|<tuple|function|?>>
     <associate|term_function_prototype|<tuple|function call|?>>
     <associate|term_linter|<tuple|1.1.2|?>>