home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 180.img / TURBOD.ZIP / TCDEMO.C < prev    next >
Text File  |  1989-05-02  |  6KB  |  156 lines

  1. /*    file <tcdemo.c>
  2.  *
  3.  *    Demonstration program to show off Turbo Debugger
  4.  *    Reads words from standard input, analyzes letter and word frequency
  5.  */
  6.  
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10.  
  11. #define BUFSIZE            128    /* length of line buffer */
  12. #define LETTERSINALPHABET       26      /* number of letters in the alphabet */
  13. #define MAXWORDLENGTH        10      /* maximum word length allowed */
  14.          
  15. static struct linfo {
  16.         unsigned int count;         /* number of occurrences of this letter */
  17.         unsigned int firstletter;       /* number of times as first letter of a word */
  18. } letterinfo[26];               /* info for each letter */;
  19.  
  20. static unsigned int wordcounts[MAXWORDLENGTH];  /* count of words of each length */
  21. static char buffer[BUFSIZE];            /* line buffer */
  22.  
  23. static int makeintowords(char *bufp);
  24. static long analyzewords(char *bufp);
  25. static void printstatistics(int nlines, int nwords, long charcount);
  26. static int readaline(void);
  27. static void showargs(int argc, char *argv[]);
  28.  
  29. /* program entry point
  30.  */
  31. int main(int argc, char **argv) {
  32.         unsigned int  nlines, nwords, wordcount;
  33.         unsigned long totalcharacters;
  34.  
  35.         nlines = 0;
  36.     nwords = 0;
  37.     totalcharacters = 0;
  38.         showargs(argc, argv);
  39.         while (readaline() != 0) {
  40.                 wordcount = makeintowords(buffer);
  41.                 nwords += wordcount;
  42.                 totalcharacters += analyzewords(buffer);
  43.                 nlines++;
  44.         }
  45.         printstatistics(nlines, nwords, totalcharacters);
  46.         return(0);
  47. }
  48.  
  49. /* make the buffer into a list of null-terminated words that end in
  50.  * in two nulls, squish out white space
  51.  */
  52. static int makeintowords(char *bufp) {
  53.         unsigned int nwords;
  54.         char *writep;
  55.  
  56.         nwords = 0;
  57.         writep = bufp;
  58.         do {
  59.                 while (*bufp == ' ')
  60.                         bufp++; /* skip white space between words */
  61.                 if (*bufp != 0)
  62.                         nwords++;       /* got the start of a word */
  63.                 while (*bufp && *bufp != ' ')
  64.                         *writep++ = *bufp++;
  65.                 if (*bufp != 0)
  66.                         bufp++; /* skip over space after word */
  67.                 *writep++ = 0;  /* word is null terminated */
  68.         }
  69.         while (*bufp);
  70.         *writep++ = 0;
  71.         return(nwords);
  72. }
  73.  
  74. /* analyze the words in the buffer
  75.  * there is a null between each word and two nulls at the end
  76.  */
  77. static long analyzewords(char *bufp) {
  78.         unsigned long charcount;
  79.     unsigned int  letterindex;
  80.  
  81.         charcount = 0;
  82.         while (*bufp != 0) {
  83.                 char first = 1;
  84.                 int wordlen = 0;
  85.                 while (*bufp != 0) {
  86.                         letterindex = toupper(*bufp) - 'A'; /* 0-based index */
  87.                         if (first) {
  88.                                 letterinfo[letterindex].firstletter++;
  89.                                 first = 0;
  90.                         }
  91.                         letterinfo[letterindex].count++;        /* count the letter */
  92.                         charcount++;
  93.                         wordlen++;
  94.                         bufp++;
  95.                 }
  96.                 wordcounts[wordlen - 1]++;      /* count a word of this length */
  97.                 bufp++; /* skip null at end of word */
  98.         }
  99.         return(charcount);
  100. }
  101.  
  102. /* display all the statistics
  103.  */
  104. static void printstatistics(int nlines, int nwords, long charcount) {
  105.         unsigned int n, count;
  106.         float    averagelength;
  107.  
  108.         if (nlines == 0)
  109.                 printf("No lines entered\n");
  110.         else {
  111.                 averagelength = (float)nwords / nlines;
  112.                 printf("Total number of letters = %ld\n", charcount);
  113.                 printf("Total number of lines = %d\n", nlines);
  114.                 printf("Total word count = %d\n", nwords);
  115.                 printf("Average number of words per line = %g\n", averagelength);
  116.                 for (n = 0; n < LETTERSINALPHABET; n++) {
  117.                         count = letterinfo[n].count;     
  118.                         if (count > 0)
  119.                                 printf("'%c' occurs %u times, %u times at start of a word\n",
  120.                                  'A' + n, count, letterinfo[n].firstletter);
  121.                 }
  122.                 for (n = 0; n < MAXWORDLENGTH; n++) {
  123.                         count = wordcounts[n];
  124.                         if (count > 0) {
  125.                                 if (count == 1)
  126.                                         printf("There is 1 word");
  127.                                 else
  128.                                         printf("There are %d words", count);
  129.                                 printf(" %d character", n + 1);
  130.                                 if (n > 0)
  131.                                         printf("s");    /* make it plural */
  132.                                 printf(" long\n");
  133.                         }
  134.                 }
  135.         }
  136. }
  137.  
  138. /* read a line from the standard input into "buffer"
  139.  * returns 1 for ok, 0 for end of file or empty line
  140.  */
  141. static int readaline() {
  142.         printf("Enter a line (empty line to end): ");
  143.         if (gets(buffer) == 0)
  144.                 return(0);      /* end of file */
  145.         return(*buffer == 0 ? 0 : 1);   /* empty line is also end */
  146. }
  147.  
  148. /* display the arguments the program was called with
  149.  */
  150. static void showargs(int argc, char *argv[]) {
  151.         printf("Arguments: ");
  152.         while (--argc)
  153.                 printf("%s ", *++argv);
  154.         printf("\n");
  155. }
  156.