training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

searchtot.c (964B)


      1 /* Exercise 8.17 */
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 
      7 #define STRINGS 10
      8 #define BUF 254
      9 
     10 int main(void)
     11 {
     12    char string[STRINGS][BUF], search[BUF / 2], *searchPtr;
     13    int i = 0, counter = 0;
     14 
     15    /* Take the strings */
     16    printf("Give me a string (%d to end): ", EOF);
     17    gets(string[i]);
     18 
     19    while( atoi(string[i++]) != -1 ) {
     20       printf("Give me a string (%d to end): ", EOF);
     21       gets(string[i]);
     22    }
     23 
     24    /* Take the search string */
     25    printf("Give me your search string: ");
     26    gets(search);
     27 
     28    for(i = 0; i < STRINGS; i++) {
     29       searchPtr = strstr(string[i], search); /* Start the search */
     30       while( searchPtr != NULL ) { /* Found all next matches */
     31 	 //printf("Match found to line %d: %s\n", i, searchPtr);
     32 	 searchPtr = strstr(searchPtr + 1, search);
     33 	 ++counter;
     34       }
     35    }
     36 
     37    if(!counter)
     38       printf("No match found!\n");
     39    else
     40       printf("Found %d matches!\n", counter);
     41 
     42    return 0;
     43 } /* E0F main */
     44