Trying word length counting

This commit is contained in:
2020-12-08 09:40:09 +02:00
parent d62ee504e6
commit ce409fccad
3 changed files with 65 additions and 0 deletions

37
wordhistogram.c Normal file
View File

@@ -0,0 +1,37 @@
#include <stdio.h>
#define IN 1
#define OUT 0
main () {
/*initialize the counting variables and array*/
int c, wl, state, i;
int wlarr[10];
/* zero out the array */
for (i=0; i<10; i++)
wlarr[i]=0;
/*take input*/
/*for (i=0; i<10; i++)
printf("On array element %d\n", i);*/
while ((c = getchar()) != EOF)
{
/*printf("state is %d\n", state);*/
if (state == OUT)
{
wlarr[i]=wl;
wl=0;
putchar('\n');
/*if you were out and a new char comes in, you're back in a word*/
state = IN;
}
if (state == IN);
putchar(c);
++wl;
if (c == ' ' || c =='\n' || c =='\t')
/*if there's a blank, newline or tab, you're out of a word*/
state = OUT;
printf("Word length is %d\n", wl);
}
}