From b86ee577dc36a408da4a108c7444ce3c744808a1 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Mon, 19 Oct 2015 05:53:58 +0200 Subject: [PATCH] Started working on exercise 7 --- vagen_till_c/ch2/exercise7.c | 30 ++++++++++++++++++++++++++++++ vagen_till_c/ch2/exercise7_ver2.c | 12 ++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 vagen_till_c/ch2/exercise7.c create mode 100644 vagen_till_c/ch2/exercise7_ver2.c diff --git a/vagen_till_c/ch2/exercise7.c b/vagen_till_c/ch2/exercise7.c new file mode 100644 index 0000000..3fb567f --- /dev/null +++ b/vagen_till_c/ch2/exercise7.c @@ -0,0 +1,30 @@ +#include + +/* My solution to exercise 7 chapter 2 from "Vägen till C". + * Not done yet... + */ + +int main() +{ + char c, all[100]; + int i = 0; + int j= 0; + + while ((c = getchar()) != EOF) + { + all[i] = c; + i++; + } + all[i] = '\0'; + + while (all[j] != EOF) + { + if (all[i] == ' ' || all[i] == '\n') + { + putchar('\n'); + } + putchar(all[j]); + j++; + } + putchar('\n'); +} diff --git a/vagen_till_c/ch2/exercise7_ver2.c b/vagen_till_c/ch2/exercise7_ver2.c new file mode 100644 index 0000000..5ecb55a --- /dev/null +++ b/vagen_till_c/ch2/exercise7_ver2.c @@ -0,0 +1,12 @@ +#include + +/* Another solution to to exercise 7, chapter 2, "Vägen till C". + * Not done yet... + */ + +int main() +{ + char c[100]; + scanf("%100[0-9a-zA-Z ]", c); + printf("%s\n", c); +}