Advertisement
Guest User

Wheres Waldo?

a guest
Feb 28th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <time.h>
  4.  
  5. #define MAX_LEN 30
  6.  
  7. int subsetComp(char waldo[], char word[]);
  8.  
  9. int main(){
  10.    
  11.     int count = 0, i, k, flag = 0;
  12.     double start = 0.0, stop = 0.0;
  13.     char word[MAX_LEN];
  14.     char waldo[MAX_LEN];
  15.     //char filename[MAX_LEN];
  16.    
  17.    
  18.     while(1){
  19.    
  20.     FILE *ifp;
  21.     ifp = fopen("Dictionary.txt", "r");
  22.    
  23.     if(ifp == NULL){
  24.            printf("\nYou dun goofed, please run the program again with Dictionary.txt in the same folder.\n");
  25.            system("PAUSE");
  26.            return 0;
  27.     }
  28.     else{
  29.          printf("Dictionary.txt opened successfully.\n");
  30.     }
  31.    
  32.    
  33.    
  34.     count = 0;
  35.     flag = 0;
  36.    
  37.    
  38.              
  39.     printf("\nPlease enter a word you want to search for (lower case only).\nUse '*' for open spaces.  Ex. sp*ce\n");
  40.     scanf("%s", waldo);
  41.     printf("\n");
  42.    
  43.     for(i = 0; i < strlen(waldo); i++){
  44.           tolower(waldo[i]);
  45.           }
  46.    
  47.     start = clock();
  48.    
  49.     while(fscanf(ifp, "%s", word) != EOF){
  50.     count++;
  51.    
  52.     //Wheres waldo?
  53.     if(subsetComp(waldo, word) == 1){
  54.     flag++;
  55.     printf("%d.\t\"%s\"", flag, word);
  56.    
  57.     for(k = 0; k < MAX_LEN - strlen(word); k++){
  58.     printf(" ");
  59.     }
  60.    
  61.     printf("\t@ line %d\n", count);
  62.     }
  63.    
  64.     }
  65.    
  66.     stop = clock();
  67.    
  68.     printf("\nFound %d matches for \"%s\" in %.3f seconds.\n\n", flag, waldo, (stop - start) / CLOCKS_PER_SEC);
  69.    
  70.     fclose(ifp);
  71.    
  72.     }
  73.    
  74.     system("PAUSE");
  75.     return 0;
  76.    
  77. }    
  78.    
  79.    
  80. int subsetComp(char waldo[], char word[]){
  81.  
  82.   int i = 0;
  83.   int lastCharPosition = 0;
  84.  
  85.   //Find last position in search term thats not a wildcard "*"
  86.   for(i = 0; i < strlen(waldo); i++){
  87.         if(waldo[i] != '*'){
  88.                      lastCharPosition = i;
  89.         }
  90.   }
  91.  
  92.   //If scanned_word is smaller than the last non-wildcard letter in waldo...
  93.   //then return FALSE (0)
  94.   if(strlen(word) < lastCharPosition + 1){
  95.                      return 0;
  96.   }
  97.  
  98.   //Go through each spot on the array until we reach the last important spot.
  99.   //IE the lastCharPosition
  100.   for(i = 0; i < lastCharPosition; i++){
  101.        
  102.         //If the spot is not a wildcard '*' ...
  103.         if(waldo[i] != '*'){
  104.                      
  105.                      //Check to see if they're different, if so then return FALSE (0)
  106.                      if(waldo[i] == word[i]){
  107.                      return 0;
  108.                      }
  109.         }
  110.   }
  111.  
  112.   //If it made it through the rest of the function, then waldo has 2 attributes.
  113.   //1. Waldo is the same length, or shorter than the dictionary word
  114.   //2. Waldo matches the dictionary word perfectly at all non-wildcard letters
  115.   //If these conditions are true, then pass TRUE (1) through the fuctions name
  116.   return 1;
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement