home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / FIND12AS.ZIP / LIB / BIGRAM.C next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  1.4 KB  |  69 lines

  1. /* bigram -- list bigrams for fast-find
  2.  
  3.    Usage: bigram < text > bigrams
  4.    
  5.    Use 'code' to encode a file using this output.
  6.  
  7.    Author: James A. Woods (jaw@riacs.edu)
  8.    Modified by David MacKenzie (djm@ai.mit.edu)
  9.    MS-DOS mods: Thorsten Ohl (ohl@gnu.ai.mit.edu)
  10.    Public domain. */
  11.  
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14.  
  15. #ifdef MSDOS
  16. #include <stdlib.h>
  17. #include <string.h>
  18. int prefix_length (char *s1, char *s2);
  19. void main (void);
  20. #else /* not MSDOS */
  21. #include <sys/param.h>
  22. #endif /* not MSDOS */
  23.  
  24. #ifndef MAXPATHLEN
  25. #define MAXPATHLEN 1024
  26. #endif
  27.  
  28. char path[MAXPATHLEN];
  29.  
  30. char oldpath[MAXPATHLEN] = " ";
  31.  
  32. void
  33. main ()
  34. {
  35.   register int count, j;
  36.  
  37.   while (fgets (path, sizeof path, stdin) != NULL)
  38.     {
  39.       path[strlen (path) - 1] = '\0'; /* Remove newline. */
  40.  
  41.       count = prefix_length (oldpath, path);
  42.       /* Output post-residue bigrams only. */
  43.       for (j = count; path[j] != '\0'; j += 2)
  44.     {
  45.       if (path[j + 1] == '\0')
  46.         break;
  47.       putchar (path[j]);
  48.       putchar (path[j + 1]);
  49.       putchar ('\n');
  50.     }
  51.       strcpy (oldpath, path);
  52.     }
  53.   exit (0);
  54. }
  55.  
  56. /* Return length of longest common prefix of strings S1 and S2. */
  57.  
  58. int
  59. prefix_length (s1, s2)
  60.      char *s1, *s2;
  61. {
  62.   register char *start;
  63.  
  64.   for (start = s1; *s1 == *s2; s1++, s2++)
  65.     if (*s1 == '\0')
  66.       break;
  67.   return s1 - start;
  68. }
  69.