Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <strings.h>
- #include <time.h>
- #define MAX_LEN 30
- int subsetComp(char waldo[], char word[]);
- int main(){
- int count = 0, i, k, flag = 0;
- double start = 0.0, stop = 0.0;
- char word[MAX_LEN];
- char waldo[MAX_LEN];
- //char filename[MAX_LEN];
- while(1){
- FILE *ifp;
- ifp = fopen("Dictionary.txt", "r");
- if(ifp == NULL){
- printf("\nYou dun goofed, please run the program again with Dictionary.txt in the same folder.\n");
- system("PAUSE");
- return 0;
- }
- else{
- printf("Dictionary.txt opened successfully.\n");
- }
- count = 0;
- flag = 0;
- printf("\nPlease enter a word you want to search for (lower case only).\nUse '*' for open spaces. Ex. sp*ce\n");
- scanf("%s", waldo);
- printf("\n");
- for(i = 0; i < strlen(waldo); i++){
- tolower(waldo[i]);
- }
- start = clock();
- while(fscanf(ifp, "%s", word) != EOF){
- count++;
- //Wheres waldo?
- if(subsetComp(waldo, word) == 1){
- flag++;
- printf("%d.\t\"%s\"", flag, word);
- for(k = 0; k < MAX_LEN - strlen(word); k++){
- printf(" ");
- }
- printf("\t@ line %d\n", count);
- }
- }
- stop = clock();
- printf("\nFound %d matches for \"%s\" in %.3f seconds.\n\n", flag, waldo, (stop - start) / CLOCKS_PER_SEC);
- fclose(ifp);
- }
- system("PAUSE");
- return 0;
- }
- int subsetComp(char waldo[], char word[]){
- int i = 0;
- int lastCharPosition = 0;
- //Find last position in search term thats not a wildcard "*"
- for(i = 0; i < strlen(waldo); i++){
- if(waldo[i] != '*'){
- lastCharPosition = i;
- }
- }
- //If scanned_word is smaller than the last non-wildcard letter in waldo...
- //then return FALSE (0)
- if(strlen(word) < lastCharPosition + 1){
- return 0;
- }
- //Go through each spot on the array until we reach the last important spot.
- //IE the lastCharPosition
- for(i = 0; i < lastCharPosition; i++){
- //If the spot is not a wildcard '*' ...
- if(waldo[i] != '*'){
- //Check to see if they're different, if so then return FALSE (0)
- if(waldo[i] == word[i]){
- return 0;
- }
- }
- }
- //If it made it through the rest of the function, then waldo has 2 attributes.
- //1. Waldo is the same length, or shorter than the dictionary word
- //2. Waldo matches the dictionary word perfectly at all non-wildcard letters
- //If these conditions are true, then pass TRUE (1) through the fuctions name
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement