From 939049115b3f1eaf18c1ecf67c7340b83822ea89 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Thu, 25 Aug 2022 02:06:33 +0000 Subject: [PATCH] feat(src/kr/s1.5):Add more examples --- src/kr_exercises/ch1/s1.5/charconst.c | 16 +++++++ src/kr_exercises/ch1/s1.5/count_chars.c | 13 ++++++ src/kr_exercises/ch1/s1.5/count_chars_v2.c | 12 +++++ src/kr_exercises/ch1/s1.5/count_lines.c | 13 ++++++ src/notes.tm | 54 ++++++++++++++++++---- 5 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 src/kr_exercises/ch1/s1.5/charconst.c create mode 100644 src/kr_exercises/ch1/s1.5/count_chars.c create mode 100644 src/kr_exercises/ch1/s1.5/count_chars_v2.c create mode 100644 src/kr_exercises/ch1/s1.5/count_lines.c diff --git a/src/kr_exercises/ch1/s1.5/charconst.c b/src/kr_exercises/ch1/s1.5/charconst.c new file mode 100644 index 0000000..b025450 --- /dev/null +++ b/src/kr_exercises/ch1/s1.5/charconst.c @@ -0,0 +1,16 @@ +#include + +/* Returns character constant of first provided char.*/ +int main() { + int c; + + while ((c = getchar()) != EOF) { + if (c == '\n') { + printf("\n"); + } else { + printf("%d\n", c); + }; + + }; + +}; diff --git a/src/kr_exercises/ch1/s1.5/count_chars.c b/src/kr_exercises/ch1/s1.5/count_chars.c new file mode 100644 index 0000000..fbc4608 --- /dev/null +++ b/src/kr_exercises/ch1/s1.5/count_chars.c @@ -0,0 +1,13 @@ +#include + +/* count characters in input; 1st version */ + +int main() { + long nc; + + nc = 0; + while (getchar() != EOF) + ++nc; + printf("%ld\n", nc); +}; + diff --git a/src/kr_exercises/ch1/s1.5/count_chars_v2.c b/src/kr_exercises/ch1/s1.5/count_chars_v2.c new file mode 100644 index 0000000..f3a0f5c --- /dev/null +++ b/src/kr_exercises/ch1/s1.5/count_chars_v2.c @@ -0,0 +1,12 @@ +#include + +/* count characters in input; 2nd version */ + +int main() { + double nc; + + for (nc = 0; getchar() != EOF; ++nc) + ; + printf("%.0f\n", nc); +}; + diff --git a/src/kr_exercises/ch1/s1.5/count_lines.c b/src/kr_exercises/ch1/s1.5/count_lines.c new file mode 100644 index 0000000..c36b5f2 --- /dev/null +++ b/src/kr_exercises/ch1/s1.5/count_lines.c @@ -0,0 +1,13 @@ +#include + +/* count lines in input */ + +int main() { + int c, nl; + + nl = 0; + while ((c = getchar()) != EOF) + if (c == '\n') + ++nl; + printf("%d\n", nl); +}; diff --git a/src/notes.tm b/src/notes.tm index 67728a6..884ee8c 100644 --- a/src/notes.tm +++ b/src/notes.tm @@ -3,6 +3,14 @@ > <\body> + <\hide-preamble> + \; + + + >||0>>|list*|list>|>>>|>>>>|> + > + + @@ -30,7 +38,7 @@ used to represent hard-to-type or invisible characters. Some commonly used escape sequences in include: - <\description-paragraphs> + <\description-aligned> >Represents the character. >Represents the character. @@ -39,11 +47,11 @@ >Represents the (i.e. \P>\Q) character. - + All other escape sequences used in are: - <\description-paragraphs> + <\description-aligned> >Represents the (bell) character. >Represents the character. @@ -69,7 +77,7 @@ >Represents a number (e.g. is the character>). - + The process of freeing memory allocated by a program but which is no longer referenced. @@ -137,7 +145,15 @@ <\description> - >Read one character at a time. + >Read one character at a time + from the input buffer. Returns as a .<\footnote> + characters have constants in the range . UTF-8 + characters return multiple constants (e.g. \<#B0\> returns ). See . + A strategy for reading the multiple bytes of a UTF-8 character is here: + . + >A generalization of . Takes three arguments: @@ -157,6 +173,23 @@ character (e.g. ) at a time. >Used for printing formatted text to console. + + Character codes include: + + <\description-aligned> + >Used with (i.e. 16-bit integers; max value of + =32\767>). + + >Used with (i.e. at least 32-bit + integers).<\footnote> + K&R 2nd Ed., Sec. 1.5 \PThe conversion specification tells + that the corresponding argument is a + integer.\Q + + + >Used with and (double + precision ). + \; @@ -178,10 +211,13 @@ > > > + > > - > + > + > > - > + > + > > > > @@ -190,8 +226,7 @@ |?>> |?>> - > - > + |prog-language||font-family||\\x>|hh>|?>> > heap|?>> stack|?>> struct|?>> > - > -- 2.39.5