/* * search.c * * * Created by xwu3 on 4/9/09. * Copyright 2009 University of Iowa. All rights reserved. * */ #include #include #define SIZE 51 void readinWords(char words[][SIZE]); /* Function for read in a dictionary file here */ void searchHoriz(char words[][SIZE],char key[]); /* Search Horizontally */ void searchVert(char words[][SIZE],char key[]); /* Search Vertically */ int main (){ char words[SIZE][SIZE]; char key[SIZE]; readinWords(words); printf("\n"); printf("Enter word [ALL CAPS]\n"); scanf("%s",key); searchHoriz(words,key); searchVert(words,key); return 0; } /* Search Vertically */ void searchVert(char words[][SIZE],char key[]) { int i, col, row; int len; len =strlen(key); for(row=0;row<50;row++){ for(col=0;col<50;col++){ i = 0; while((i < len) && (key[i] == words[row+i][col])) { i++; if(i == len) { printf("found %s vertical at row %d col %d\n",key,row,col); } } } } } /* Search Horizontally */ void searchHoriz(char words[][SIZE],char key[]) { int i, col, row; int len; len =strlen(key); for(row=0;row<50;row++){ for(col=0;col<50;col++){ i = 0; while((i < len) && (key[i] == words[row][col+i])) { i++; if(i == len) { printf("found %s horizontal at row %d col %d\n",key,row,col); } } } } } void readinWords(char words[][SIZE]){ FILE *fptr; char buffer[SIZE]; int row = 0, col = 0; int count=0; if((fptr = fopen("words.txt","r"))==NULL) { printf("Could not open words.txt\n"); } else { printf("Opened words.txt\n"); while(row < 50){ fscanf(fptr, "%s", buffer); sprintf(words[row], "%s", buffer); count=row; row++; } fclose(fptr); } for(row=0;row<50;row++){ for(col=0;col<50;col++){ printf("%c", words[row][col]); } printf("\n"); } }