From d6f81778ef0bcc71fe84f19a8fa03063c24ea135 Mon Sep 17 00:00:00 2001 From: Reina Harrington-Affine Date: Thu, 14 Aug 2025 19:23:28 +0000 Subject: [PATCH] Completed and improved ex1_10 using a switch function. --- KNR C/ex1_10_improved.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 KNR C/ex1_10_improved.c diff --git a/KNR C/ex1_10_improved.c b/KNR C/ex1_10_improved.c new file mode 100644 index 0000000..acfdb64 --- /dev/null +++ b/KNR C/ex1_10_improved.c @@ -0,0 +1,30 @@ +#include + +/*Alternative (challenge) implementation of exercise 1-10 using a switch statement + * as opposed to a series of if statements*/ + +int main(){ +int a = 0; /*Initializing a as zero; this doesn't matter because getchar is called at the top of the loop */ + + while (a != EOF){ + + a = getchar(); + + switch (a){ + case '\t': + printf ("\\t"); + break; + case '\\': + printf ("\\\\"); + break; + case '\b': + printf ("\\b"); + break; + case EOF: /*Required case for checking if a is EOF, since the loop actually tests whether a = EOF before refreshing a, thus allowing us to enter the loop with the value of a set to EOF */ + break; + default: + putchar (a); + } + } + return 0; +}