From d360dfb82e52a96fe565ec441fab1f63366466b5 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Thu, 25 Aug 2022 05:09:16 +0000 Subject: [PATCH 1/1] update(src/kr/s1.5):Add exercises --- src/kr_exercises/ch1/s1.5/count_btn.c | 36 ++++++++++++++++++ .../ch1/s1.5/e1-10..verbose_escchar.c | 29 +++++++++++++++ .../ch1/s1.5/e1-9..squeeze_blanks.c | 37 +++++++++++++++++++ src/notes.tm | 36 +++++++++++++++++- 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/kr_exercises/ch1/s1.5/count_btn.c create mode 100644 src/kr_exercises/ch1/s1.5/e1-10..verbose_escchar.c create mode 100644 src/kr_exercises/ch1/s1.5/e1-9..squeeze_blanks.c 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 index 0000000..f36f80b --- /dev/null +++ b/src/kr_exercises/ch1/s1.5/count_btn.c @@ -0,0 +1,36 @@ +/* Desc: Counts blanks, tabs, and newlines */ +/* Ref/Attrib: K&R 2nd Ed. Exercise 1-8 */ + +#include +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 index 0000000..424fa13 --- /dev/null +++ b/src/kr_exercises/ch1/s1.5/e1-10..verbose_escchar.c @@ -0,0 +1,29 @@ +/* Desc: Print tabs, backspaces, and backslashes with escape backslashes */ +/* Ref/Attrib: K&R 2nd Ed. Exercise 1-10 */ + +#include +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 index 0000000..3635604 --- /dev/null +++ b/src/kr_exercises/ch1/s1.5/e1-9..squeeze_blanks.c @@ -0,0 +1,37 @@ +/* Desc: Squeezes consecutive blanks */ +/* Ref/Attrib: K&R 2nd Ed. Exercise 1-9 */ + +#include +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+ */ diff --git a/src/notes.tm b/src/notes.tm index 884ee8c..0d4cac6 100644 --- a/src/notes.tm +++ b/src/notes.tm @@ -16,6 +16,40 @@ <\description-compact> + An expression in which a variable is set to a value. + For example, in the expression >, the variable + is set to the value because is to the left of the equals + sign \ \P\Q. The value of the entire expression is equal to the + value of the left hand side (i.e. in this example) after the + assignment is performed. For example, the following C code will print + \P\Q: + + <\cpp-code> + #include \stdio.h\ + + int main() { + + \ \ int c; + + \ \ if( (c = 7) == 7 ) + + \ \ \ \ printf("true"); + + \ \ else + + \ \ \ \ printf("false"); + + }; + + + As another example, the following lines are equivalent: + + <\cpp-code> + a = b = c = 0; + + a = ( b = ( c = 0 ) ); + + >A construct that establishes an association between a particular variable, function, or type and its attributes. (See @@ -221,7 +255,7 @@ > > > - > + > |?>> |?>>