/**************************************************/ /* Jeff Offutt - June 1989 */ /* */ /* stutter checks for repeat words in a text file.*/ /* */ /* stutter will accept standard input or a list */ /* of file names. */ /**************************************************/ #include #define MAXWDLEN 100 #define EOS '\0' #define EOL '\n' #define TRUE 1 #define FALSE 0 /*************************************************/ /* main parses the arguments, decides if stdin */ /* or a file name, and calls Stut. */ /*************************************************/ main (argc, argv) int argc; char *argv []; { FILE *fp, *fopen (); if (argc == 1) /* No file names, use stdin */ Stut (stdin); else while (--argc > 0) /* Use all file names. */ if ((fp = fopen (*++argv, "r")) == NULL) { printf ("stutter: can't open %s\n", *argv); break; } else { Stut (fp); fclose (fp); } } /*************************************************/ /*************************************************/ Stut (FP) FILE *FP; { char c, curword [100], lastword [100]; int i = 0; int linecnt = 1; int lastdelimit = TRUE; lastword [0] = EOS; while ((c = getc (FP)) != EOF) { if (c == EOL) linecnt++; if (IsDelimit (c)) { if (lastdelimit) continue; lastdelimit = TRUE; curword [i] = EOS; i = 0; if (IsEqualWd (lastword, curword)) { printf ("Repeated word on line %d: %s %s\n", linecnt, lastword, curword); } else AssignWd (curword, lastword); } else { lastdelimit = FALSE; curword [i++] = c; } } putc ('\n', stdout); } /*************************************************/ /*************************************************/ int IsDelimit (C) char C; { static char delimits [] = {EOS, EOL, ' ', ' ', ',', '.', '!', '-', '+', '=', ';', ':', '?', '&', '{', '}', '\\'}; int N = 17; /* Number of deliminators. */ int i; for (i = 0; i <= N-1; i++) if (C == delimits [i]) return (TRUE); return (FALSE); } /*************************************************/ /*************************************************/ int IsEqualWd (W1, W2) char *W1, *W2; { int i = 0; while ((W1[i] != EOS) && (W2[i] != EOS)) { if (W1 [i] != W2 [i]) return (FALSE); i++; } if ((W1[i] == EOS) && (W2[i] == EOS)) return (TRUE); else return (FALSE); } /*************************************************/ /*************************************************/ AssignWd (OldW, NewW) char *OldW, *NewW; { int i=0; do NewW [i] = OldW [i]; while (OldW [i++] != EOS); }