diff --git a/alsocharcount.c b/alsocharcount.c new file mode 100644 index 0000000..285ffe3 --- /dev/null +++ b/alsocharcount.c @@ -0,0 +1,13 @@ +#include +/*ripped off the internet; that's why it's so elegant*/ +main (){ + int c; + while ((c = getchar()) != EOF) { + if (c == ' ') { + while ((c = getchar()) == ' '); + putchar(' '); + if (c == EOF) break; + } + putchar(c); + } +} \ No newline at end of file diff --git a/wordcount.c b/wordcount.c new file mode 100644 index 0000000..0a6a65e --- /dev/null +++ b/wordcount.c @@ -0,0 +1,32 @@ +#include + +#define IN 1 +#define OUT 0 + +main(){ + /*these are the current char, new lines, words, and chars*/ + int c, nl, nw, nc, state; + + state = OUT; + nl = nw = nc = 0; + while ((c=getchar()) != EOF){ + /*count a new character*/ + if (state == OUT) + putchar('\n'); + ++nc; + if (state == IN); + putchar(c); + if (c =='\n') + ++nl; + if (c == ' ' || c =='\n' || c =='\t') + /*if there's a blank, newline or tab, you're out of a word*/ + state = OUT; + else if (state == OUT) + { + /*if you were out and a new char comes in, you're back in a word*/ + state = IN; + ++nw; + } + } + printf ("Counted %d lines, %d words and %d characters.\n", nl, nw, nc ); +} \ No newline at end of file