]> zdv2.bktei.com Git - BK-2023-05.git/commitdiff
update(src/kr/s1.5):Add exercises
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Thu, 25 Aug 2022 05:09:16 +0000 (05:09 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Thu, 25 Aug 2022 05:09:16 +0000 (05:09 +0000)
src/kr_exercises/ch1/s1.5/count_btn.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.5/e1-10..verbose_escchar.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.5/e1-9..squeeze_blanks.c [new file with mode: 0644]
src/notes.tm

diff --git a/src/kr_exercises/ch1/s1.5/count_btn.c b/src/kr_exercises/ch1/s1.5/count_btn.c
new file mode 100644 (file)
index 0000000..f36f80b
--- /dev/null
@@ -0,0 +1,36 @@
+/* Desc: Counts blanks, tabs, and newlines */
+/* Ref/Attrib: K&R 2nd Ed. Exercise 1-8 */
+
+#include <stdio.h>
+int main() {
+  int c, cb, ct, cnl;
+
+  /* Initialize counters */
+  cb = 0;
+  ct = 0;
+  cnl = 0;
+
+  /* User prompt loop. */
+  while((c = getchar()) != EOF) {
+    /* Detect blanks character */
+    if(c == ' ')
+      ++cb;
+    
+    /* Detect tab character */
+    if(c == '\t')
+      ++ct;
+
+    /* Detect newline character */
+    if(c == '\n')
+      ++cnl;
+  };
+
+  /* Print results. */
+  printf("Blnk:\t%d\n", cb);
+  printf("Tabs:\t%d\n", ct);
+  printf("Nwln:\t%d\n", cnl);
+  printf("\nDone.\n");
+};
+
+/* Author: Steven Baltakatei Sandoval
+   License: GPLv3+ */
diff --git a/src/kr_exercises/ch1/s1.5/e1-10..verbose_escchar.c b/src/kr_exercises/ch1/s1.5/e1-10..verbose_escchar.c
new file mode 100644 (file)
index 0000000..424fa13
--- /dev/null
@@ -0,0 +1,29 @@
+/* Desc: Print tabs, backspaces, and backslashes with escape backslashes */
+/* Ref/Attrib: K&R 2nd Ed. Exercise 1-10 */
+
+#include <stdio.h>
+int main() {
+  int c;
+
+  /* User prompt loop. */
+  while((c = getchar()) != EOF) {
+
+    if(c == '\t') {
+      /* If c is tab */
+      printf("\\t");
+    } else if(c == '\b') {
+      /* If c is backspace */
+      printf("\\b");
+    } else if(c == '\\') {
+      /* If c is backslash */
+      printf("\\\\");
+    } else
+      putchar(c);
+    
+  };
+
+  printf("\nDone.\n");
+};
+
+/* Author: Steven Baltakatei Sandoval
+   License: GPLv3+ */
diff --git a/src/kr_exercises/ch1/s1.5/e1-9..squeeze_blanks.c b/src/kr_exercises/ch1/s1.5/e1-9..squeeze_blanks.c
new file mode 100644 (file)
index 0000000..3635604
--- /dev/null
@@ -0,0 +1,37 @@
+/* Desc: Squeezes consecutive blanks */
+/* Ref/Attrib: K&R 2nd Ed. Exercise 1-9 */
+
+#include <stdio.h>
+int main() {  
+  int c, cb;
+
+  /* Initialize counters */
+  cb = 0; // count of blanks
+
+  /* User prompt loop. */
+  while((c = getchar()) != EOF) {
+
+    if(c == ' ') {
+      /* If c is blank */
+      ++cb;
+    } else if (cb <= 0) {
+      /* If last round was not a blank AND c is not a blank */
+      putchar(c);
+    } else {
+      /* Last round was a blank AND c is not a blank */ 
+      cb = 0;
+      printf(" ");
+      putchar(c);
+    };
+  };
+  
+  if(cb > 0) {
+    /* If EOF received after blank(s) */
+    printf(" ");
+  };
+
+  printf("\nDone.\n");
+};
+
+/* Author: Steven Baltakatei Sandoval
+   License: GPLv3+ */
index 884ee8cd24c54322bbc17056f582f626670129b8..0d4cac65d7c447da26bf10a84a3094b2b31007e4 100644 (file)
   <section|Terminology>
 
   <\description-compact>
+    <item*|assignment>An expression in which a variable is set to a value.
+    For example, in the expression <hgroup|<cpp|x = 1>>, the variable <cpp|x>
+    is set to the value <cpp|1> because <cpp|x> is to the left of the equals
+    sign \ \P<cpp|=>\Q. The value of the entire expression is equal to the
+    value of the left hand side (i.e. <cpp|x> in this example) after the
+    assignment is performed. For example, the following C code will print
+    \P<cpp|true>\Q:
+
+    <\cpp-code>
+      #include \<less\>stdio.h\<gtr\>
+
+      int main() {
+
+      \ \ int c;
+
+      \ \ if( (c = 7) == 7 )
+
+      \ \ \ \ printf("true");
+
+      \ \ else
+
+      \ \ \ \ printf("false");
+
+      };
+    </cpp-code>
+
+    As another example, the following lines are equivalent:
+
+    <\cpp-code>
+      a = b = c = 0;
+
+      a = ( b = ( c = 0 ) );
+    </cpp-code>
+
     <item*|declaration<label|term declaration>>A construct that establishes
     an association between a particular variable, function, or type and its
     attributes. (See <hlink|cppreference.com|https://en.cppreference.com/w/c/language/declarations>
     <associate|func getchar|<tuple|1.2.1|?>>
     <associate|func putchar|<tuple|3|?>>
     <associate|ref includehelp-oct-hex|<tuple|1.1.1|?>>
-    <associate|term declaration|<tuple|1.1|?>>
+    <associate|term declaration|<tuple|assignment|?>>
     <associate|term definition|<tuple|declaration<label|term declaration>|?>>
     <associate|term enumeration|<tuple|definition<label|term definition>|?>>
     <associate|term escape_sequence|<tuple|enumeration<label|term