Skip to content

Instantly share code, notes, and snippets.

@funrep
Created September 27, 2012 18:17
Show Gist options
  • Save funrep/3795520 to your computer and use it in GitHub Desktop.
Save funrep/3795520 to your computer and use it in GitHub Desktop.
k&r exercise 1-13 attempt
#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