Code:
#include <stdio.h>
#include <string.h>
#include <string.h>
#define WORDSEARCH "WordSearch.txt"//The puzzle
#define WORDLIST "WordList.txt" //The list of Words
#define ROWS 15 //# of Rows
#define COLS 15 //# of Columns
typedef struct
{
int row;
int col;
} position;
//Declaring the Functions that are going to be used
void Print_Puzzle (FILE *wrdsearch ,FILE *wrdlist,char wordsearch[ROWS][COLS]);
int FIND (char grid[ROWS][COLS],char *word);
int SEARCH (char grid[ROWS][COLS],char *word,position *start,position *end);
int main ( )
{
FILE *wrdlist;
FILE *wrdsearch;
FILE *solutions;
int i;
int x;
char wordtmp[80]; //Temp is to hold words from the WordList
char *nptr;
char grid[ROWS][COLS];
//Opening the WordSearch
wrdsearch = fopen( WORDSEARCH , "r");
if (!wrdsearch)
{
printf("File not Found");
exit (0);
}
//Opening the WordList
wrdlist = fopen( WORDLIST , "r");
if (!wrdlist)
{
printf("File not Found");
exit (0);
}
//Opening the Output
solutions = fopen( "output.txt" , "w");
if (!solutions)
{
printf("File not Found");
exit (0);
}
Print_Puzzle(wrdsearch,wrdlist,grid);
fclose(wrdsearch);
wrdsearch = NULL ;
for ( i = 0; !(feof(wrdlist)); i++ )
{
fgets(wordtmp,sizeof wordtmp,wrdlist);
if ( (nptr = strchr (wordtmp,'\n' ) ) != NULL )
{
*nptr = '\0';
x = FIND ( grid, wordtmp );
}
}/* Strchr() returns a pointer to the first occurrence of character
** located within s. If character c does not occur in the string,
** strchr() returns a null pointer. */
return 0;
}
//PRINTING OUT THE PUZZLE AND THE WORDS, CAN PRINT ANY SIZE PUZZLE
void Print_Puzzle(FILE *wrdsearch,FILE *wrdlist,char WordSearch[ROWS][COLS])
{
int row , column, i;
char WordList[80];
printf("THE PUZZLE...size %i by %i\n", ROWS, COLS);
printf("____________________________\n");
for ( row=0 ; row < ROWS ; row++)
{
for( column = 0; column < COLS ; column++ )
{
fscanf(wrdsearch, "%c", &WordSearch[row][column]);
if(WordSearch[row][column] == '\n' ||
WordSearch[row][column] == ' ' )
{
column--;
continue;
}
printf("%c ", WordSearch[row][column]);
}
printf("\n");
} printf("\n\n\n");
//PRINTING OUT THE WORDS
printf("The Words to Search For\n");
printf("_______________________\n");
for ( i = 0; !(feof(wrdlist)); i++ )
{
fscanf(wrdlist, "%s", WordList);
printf("%s\n", WordList);
}
}
// The find function searches a word recieved from main
// Starting at each position in the grid
int FIND ( char grid[ROWS][COLS], char *word )
{
int row,column;
printf("The subscripts are by [across,vertical]\n\n\n");
for ( row=0 ; row<ROWS ; row++ )
{
for ( column=0 ; column<COLS ; column++ )
{
position start = { row, column };
position end;
if ( SEARCH( grid, word, &start, &end ) )
{
printf( "%s starts at [%d,%d] and ends at [%d,%d]\n\n",
word, start.row, start.col, end.row, end.col );
if ( !(SEARCH( grid, word, &start, &end )))
printf("%s IS NOT IN THE PUZZLE",word);
return 0;
}
}
}
return 1;
}
// The Find Function sends SEARCH a start position and
// Search will look for the word in 8 possible directions
int SEARCH (char grid[ROWS][COLS],char *word,position *start,position *end)
{
int row , column ;
for ( row = -1 ; row <= 1 ; row++ )
{
for ( column = -1 ; column <= 1 ; column++ )
{
if ( row != 0 || column != 0 )
{
char *wrdp = word;
*end = *start;
/* This While loop checks to see that the letters match
** and that the end position is not out of the puzzle */
while ( grid[end->row][end->col] == *wrdp &&
end->row >= 0 && end->row < ROWS
&&
end->col >= 0 && end->col < COLS )
{
wrdp++;
if ( *wrdp == '\0' )
{
return 1; /* There is a match */
}
/* This will check the next
** position in the current
** direction for colums and rows */
end->col += column;
end->row += row;
}
}
}
}
return 0; /* The Word is not found / is not in Puzzle */
}
output: