Datei: p6-1.c


    1   /*
    2    *      p6-1.c
    3    *      Beispielprogramm 1, Abschnitt 6
    4    *      Alle Zeilen mit Suchmuster finden
    5    */
    6   
    7   #include <stdio.h>
    8   
    9   #define MAXLINE 1000
   10   
   11   int getline(char [], int);
   12   int index(char [], char []);
   13   
   14   void main(void)
   15   {
   16     char line[MAXLINE];
   17     
   18     while (getline(line, MAXLINE - 1) > 0)
   19       if (index(line, "the") > 0)
   20         printf("%s", line);
   21   
   22   } /* main() */
   23   
   24   /* Zeile in s ablegen, Länge liefern */
   25   int getline(char s[], int lim)
   26   { 
   27     int c, i;
   28     
   29     i = 0;
   30     while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
   31       s[i++] = c;
   32     if (c == '\n')
   33       s[i++] = c;
   34     s[i] = '\0';
   35     return(i);
   36   
   37   } /* getline() */
   38   
   39   /* Position von t in s liefern, -1 falls nicht da */
   40   int index (char s[], char t[])    
   41   { 
   42     int i, j, k;
   43     
   44     for (i = 0; s[i] != '\0'; i++) {
   45       for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
   46         ;
   47       if (t[k] == '\0')
   48         return(i);
   49     }
   50     return(-1);
   51     
   52   } /* index() */


Erzeugt von c2html 1.01