Added more stupid coding files

This commit is contained in:
2020-12-07 09:14:40 +02:00
parent 4811964a71
commit 1b537cf150
2 changed files with 45 additions and 0 deletions

13
alsocharcount.c Normal file
View File

@@ -0,0 +1,13 @@
#include <stdio.h>
/*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);
}
}

32
wordcount.c Normal file
View File

@@ -0,0 +1,32 @@
#include <stdio.h>
#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 );
}