Created
September 27, 2012 18:17
-
-
Save funrep/3795520 to your computer and use it in GitHub Desktop.
k&r exercise 1-13 attempt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define OUT 1 /* outside a word */ | |
#define IN 0 /* inside a word */ | |
#define MAX 30 /* max words */ | |
main() | |
{ | |
int c, i, j, state, nw; | |
int words[MAX]; | |
int wlen; | |
nw = 0; | |
for (i = 0; i < 30; ++i) | |
words[i] = 0; | |
state = OUT; | |
while ((c = getchar()) != EOF) { | |
if ( c == ' ' || c == '\t' || c == '\n' ) { | |
state = OUT; | |
} | |
if (state == OUT) { | |
state = IN; | |
++nw; | |
} | |
if (state == IN) | |
++words[nw]; | |
} | |
for (i = 0; i < 30; ++i) { | |
j = 0; | |
wlen = 0; | |
printf("word %d = ", i); | |
wlen = words[i]; | |
for (j = 0; j <= wlen; ++j) { | |
printf("#"); | |
if (j == wlen) | |
printf("\n"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment