Contents

  1. proj4.c
  2. words.h
  3. words.c

proj4.c 1/3

[
top][prev][next]
/************************************************
 * File:  proj4.c                               *
 * Date:  17 Nov 2005                           *
 * Sect:  201 Staff                             *
 * Name:  Natalie Podrazik                      *
 * Email: natalie2@umbc.edu                     *
 *                                              *
 *   This file contains the main driver for the *
 * 'Word Search' program.  It searches for      *
 * words in a given word search via recursion.  *
 *                                              *
 * ** This program requires the following       *
 *    command line arguments:                   *
 *   argv[0] : <executable name>                * 
 *   argv[1] : <file containing word search>    *
 *   argv[2] : <file containing words to find>  *
 *   argv[3] : <file to print results to>       *
 ************************************************/

#include <stdio.h>
#include <stdlib.h>

#include "words.h"

int main(int argc, char * argv[])
{
   
   FILE * jumbleIFP;     /* the input file for the grid of jumbled letters */
   FILE * wordsIFP;      /* the input file for the words to search for */
   int numWords;         /* number of words to search for */   
   WORD * findMe;        /* array to contain info about words-to-find */
   char jumbleArray[ARRAYSIZE][ARRAYSIZE]; /* grid of jumbled letters */

   /* make sure they enter correct # of cmdline args */
   if (argc != 4)
   {
      fprintf(stderr,"\nERROR: This program requires exactly 4 cmdline args:\n");
      fprintf(stderr, "<executable file> <crossword file> <words file> ");
      fprintf(stderr,"<output file>\n\n");
      exit(-2);
   }
  
   PrintGreeting();

   /* open the crossword puzzle, initialize the jumble array to contain what's *
    * in that file, then close the file since we're done using it. */
   jumbleIFP = OpenForReading(argv[1]);
   ReadInJumble(jumbleIFP, jumbleArray); 
   fclose(jumbleIFP);

   /* open the file that contains the list of words to search for */
   wordsIFP = OpenForReading(argv[2]);
   
   /* do an initial run through the file to get the # of word, then rewind */
   numWords = GetNumWords(wordsIFP);
   rewind(wordsIFP);

   /* allocate memory for that array */
   findMe = AllocateWORDMem(numWords);

   /* starting at the beginning of the file, fill in the words array with *
    * the word to find, and make sure to clear the contents of the WORD struct. */
   FillFindArray(wordsIFP, findMe, numWords); 

   /* all done with the file, close it */
   fclose(wordsIFP);

   /* find all the words in the jumbled array. */
   FindWords(jumbleArray, findMe, numWords);

   /* all done, print those answers out */
   PrintAnswers(argv[3], findMe, numWords);  
   printf("\n");   

   /* print the jumbled array to stdout */
   PrintJumble(jumbleArray);
   printf("\n\n");

   /* also print the list of words to find to sdout. */
   PrintWordsToFind(findMe, numWords);
   printf("\n");

   free(findMe);

   return 0;
}


   
  
   

words.h 2/3

[
top][prev][next]
/************************************************
 * File:  words.h                               *
 * Date:  17 Nov 2005                           *
 * Sect:  201 Staff                             *
 * Name:  Natalie Podrazik                      *
 * Email: natalie2@umbc.edu                     *
 *                                              *
 *   This file contains the prototypes for the  *
 * 'Word Search' program's functions.           *
 ************************************************/

#define ARRAYSIZE 17    /* 17 x 17 array */
#define MAXWORDLEN 16   /* words won't be longer than 15 + 1 for '\0'*/

/* struct for a word to find. */
typedef struct word
{
   char word[MAXWORDLEN];
   int  startRow;
   int  startCol;
   char direction;
}WORD;


/*********************************************
 * OpenForReading()                          *
 *  inputs:                                  *
 *    char * file - the input file name      *
 *                                           *
 *  outputs: returns the file * for that file*
 *                                           *
 *  Tries to open the filename passed in and *
 * returns the file pointer.  If the file    *
 * can't be opened for reading, it prints to *
 * stderr and exits.                         *
 *********************************************/
FILE * OpenForReading(char * file);


/*********************************************
 * ReadInJumble()                            *
 *  inputs:                                  *
 *    FILE * jumbleIFP - filePtr to the file *
 *            that contains the word search  *
 *    char jumble[][ARRAYSIZE] - the array to*
 *            hold the word search           *
 *                                           *
 *  outputs: jumble array holds word search  *
 *                                           *
 *  Reads in the word search, one character  *
 * at a time, into the jumble array.         *
 *********************************************/
void ReadInJumble(FILE * jumbleIFP, char jumble[][ARRAYSIZE]);


/*********************************************
 * PrintJumble()                             *
 *  inputs:                                  *
 *    char jumble[][ARRAYSIZE] - the array   *
 *            that holds the word search     *
 *                                           *
 *  outputs: prints jumble array to stdout   *
 *                                           *
 *  Prints the entire jumble array to stdout *
 *********************************************/
void PrintJumble(char jumble[][ARRAYSIZE]);


/*********************************************
 * OpenForWriting()                          *
 *  inputs:                                  *
 *    char * file - filename to open         *
 *                                           *
 *  outputs:                                 *
 *    returns a FILE * to the file opened    *
 *                                           *
 *  Opens a file with the filename passed in,*
 * returns the fileptr to that file.         *
 *  Note: This may seem like a somewhat      *
 *   pointless function, but I made it to be *
 *   consistent, since I wrote a functio for *
 *   OpenForReading(), and it only makes     *
 *   sense to have one for writing, too.     *
 *********************************************/
FILE * OpenForWriting(char * file);


/*********************************************
 * AllocateWORDMem()                         *
 *  inputs:                                  *
 *    int quantity - # of words to allocate  *
 *       space for                           *
 *                                           *
 *  outputs:                                 *
 *    returns a WORD * <-- array of WORDs    *
 *                                           *
 *  Allocates space (and makes sure that     *
 * space was allocated successfully) for     *
 * quantity number of words, and returns the *
 * pointer to the space allocated.           *
 *********************************************/
WORD * AllocateWORDMem(int quantity);


/*********************************************
 * FillFindArray()                           *
 *  inputs:                                  *
 *    FILE * ifp - filePtr to the file that  *
 *      has the list of words to find        *
 *    WORD * findMe - array of WORDs         *
 *    int numWords - size of findMe          *
 *                                           *
 *  outputs:                                 *
 *    intializes the findMe array to contain *
 *   what's in the file pted to by ifp, and  *
 *   clears all other fields in that WORD    *
 *   struct                                  *
 *                                           *
 *  For every word in the findMe array, it   *
 * reads in a word from the file pointed to  *
 * by ifp and initalizes its startRow and    *
 * startCol to be 0, as well as its direction*
 * to be UNKNOWN.                            *
 *********************************************/
void FillFindArray(FILE * ifp, WORD * findMe, int numWords);


/*********************************************
 * GetNumWords()                             *
 *  inputs:                                  *
 *    FILE * ifp - pointer to the input file *
 *      of words to find                     *
 *                                           *
 *  outputs:                                 *
 *    returns the number of words in the file*
 *                                           *
 *  This function just counts the number of  *
 * words in the file.  NOTE that it reads up *
 * until the end of the file, and the filePtr*
 * must be rewound if you'd like to deal with*
 * it anymore.                               *
 *********************************************/
int GetNumWords(FILE * ifp);


/*********************************************
 * FindWords()                               *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - the    *
 *      array that holds the wordsearch chars*
 *    WORD * findMe - the array of words to  *
 *      search for                           *
 *    int numWords - size of findMe array    *
 *                                           *
 *  outputs:                                 *
 *    modifies the findMe array to contain   *
 *   where each findMe[i]'s word was found,  *
 *   as in its row, col, and direction.      *
 *                                           *
 *  This function loops through each word in *
 *  the findMe array and searches for the    *
 *  word in 4 directions:                    *
 *   1. horizontally forward  (left)         *
 *   2. horizontally backward (right)        *
 *   3. vertically forward    (down)         *
 *   4. vertically backward   (up)           *
 *  Each of these checks are calls to a      *
 *  different recursive function.  If none of*
 *  these calls find the word, the word is   *
 *  left to be 'unfound'.                    *
 *********************************************/
void FindWords(char jumbleArray[][ARRAYSIZE], WORD * findMe, int numWords);


/*********************************************
 * HorizontalBwdSearch()                     *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * horizontal ordering of a string (LEFT)    *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain ARRAYSIZE - 1 so the recursion   *
 *  can start at the bottom right corner of  *
 *  the array and work its way backward. The *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int HorizontalBwdSearch(char jumbleArray[][ARRAYSIZE], int row, 
                     int col, char * word, int * foundRow,
			int * foundCol, int origLen);


/*********************************************
 * HorizontalFwdSearch()                     *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * horizontal ordering of a string (RIGHT)   *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain 0 so the recursion               *
 *  can start at the top left corner of      *
 *  the array and work its way forward. The  *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int HorizontalFwdSearch(char jumbleArray[][ARRAYSIZE], int row, 
                     int col, char * word, 
			int * foundRow, int * foundCol, int origLen);

/*********************************************
 * PrintAnswers()                            *
 *  inputs:                                  *
 *    char * file - filename to print to     *
 *    WORD * words - word array              *
 *    int numWords - size of word array      *
 *                                           *
 *  outputs:                                 *
 *    prints the results of the wordsearch   *
 *   to the file opened named filename       *
 *                                           *
 *  This function loops through the words    *
 * array and prints the results of the search*
 * for that word.  If the word was found, the*
 * direction of its orientation, as well as  *
 * its [row][col] coordinates are printed.   *
 * If the word was not found, the [row][col] *
 * printed is 0 and 0, and the orientation is*
 * "Not Present".                            *
 *********************************************/
void PrintAnswers(char * file, WORD * words, int numWords);


/*********************************************
 * VerticalFwdSearch()                       *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * vertical ordering of a string (DOWN)      *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain 0 so the recursion               *
 *  can start at the top left corner of      *
 *  the array and work its way forward. The  *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int VerticalFwdSearch(char jumbleArray[][ARRAYSIZE], int row,
                      int col, char * word, int * foundRow, int * foundCol,
                      int origLen);



/*********************************************
 * VerticalBwdSearch()                       *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * vertical ordering of a string (UP)        *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain ARRAYSIZE-1 so the recursion     *
 *  can start at the bottom right corner of  *
 *  the array and work its way forward. The  *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int VerticalBwdSearch(char jumbleArray[][ARRAYSIZE], int row,
                     int col, char * word, int * foundRow,
                     int * foundCol, int origLen);


/*********************************************
 * PrintWordsToFind()                        *
 *  inputs:                                  *
 *    WORD * findMe - word array             *
 *    int numWords - size of word array      *
 *                                           *
 *  outputs:                                 *
 *    prints a list of words to find to      *
 *    stdout                                 *
 *                                           *
 *  This function loops through the words    *
 * array and prints its .word field to stdout*
 *********************************************/
void PrintWordsToFind(WORD * findMe, int numWords);


/*********************************************
 * PrintGreeting()                           *
 *  inputs: (none)                           *    
 *                                           *
 *  outputs:                                 *
 *    prints a greeting to the user (stdout) *
 *                                           *
 *  This function greets the user.           *
 *********************************************/
void PrintGreeting(void);


/*********************************************
 * GetDirectionStr()                         *
 *  inputs:                                  *    
 *      char d - direction of a string       *
 *                                           *
 *  outputs:                                 *
 *    returns the string that char direction *
 *     represents                            *
 *                                           *
 *  This function takes the direction as one *
 * character and returns a printable string  *
 * of what that character represents.  Ex:   *
 *    if (d == 'U')                          *
 *     GetDirectionStr() returns "Up"        *
 *    if (d == '?')                          *
 *     GetDirectionStr() returns "Not Present"*
 *********************************************/
char * GetDirectionStr(char d);

words.c 3/3

[
top][prev][next]
/************************************************
 * File:  words.c                               *
 * Date:  17 Nov 2005                           *
 * Sect:  201 Staff                             *
 * Name:  Natalie Podrazik                      *
 * Email: natalie2@umbc.edu                     *
 *                                              *
 *   This file contains the definitions for the *
 * 'Word Search' program's functions.           *
 ************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "words.h"

/* directions - chars and strings */
#define UP             'U'
#define UP_STRING      "Up"
#define DOWN           'D'
#define DOWN_STRING    "Down"
#define LEFT           'L'
#define LEFT_STRING    "Left"
#define RIGHT          'R'
#define RIGHT_STRING   "Right"
#define DEFAULT        '?'
#define DEFAULT_STRING "Not Present"

/* just in case */
#define UNKNOWN_STRING "(unknown)"

/* in case you didn't already know... */
#define TRUE  1
#define FALSE 0

/* to match the project description's output */
#define NUM_SPACES 22


/*********************************************
 * OpenForReading()                          *
 *  inputs:                                  *
 *    char * file - the input file name      *
 *                                           *
 *  outputs: returns the file * for that file*
 *                                           *
 *  Tries to open the filename passed in and *
 * returns the file pointer.  If the file    *
 * can't be opened for reading, it prints to *
 * stderr and exits.                         *
 *********************************************/
FILE * OpenForReading(char * file)
{
   FILE * ifp;
 
   /* try to open that file, exit if you can't */
   ifp = fopen(file, "r");
   if (ifp == NULL) 
   {
      fprintf(stderr, "\nERROR! Could not open file %s.\n", file);
      exit(-1);
   }

   return ifp;
}


/*********************************************
 * ReadInJumble()                            *
 *  inputs:                                  *
 *    FILE * jumbleIFP - filePtr to the file *
 *            that contains the word search  *
 *    char jumble[][ARRAYSIZE] - the array to*
 *            hold the word search           *
 *                                           *
 *  outputs: jumble array holds word search  *
 *                                           *
 *  Reads in the word search, one character  *
 * at a time, into the jumble array.         *
 *********************************************/
void ReadInJumble(FILE * jumbleIFP, char jumble[][ARRAYSIZE])
{
   int i, j;
   char garbage;  /* to grab the newlines in the file */

   /* first clear the array, just in case. */
   for (i = 0; i < ARRAYSIZE; i++)
   {
      for (j = 0; j < ARRAYSIZE; j++)   
      {
         jumble[i][j] = ' ';
      }
   }

   /* now scan strings from the file */
   for (i = 1; i < ARRAYSIZE - 1; i++)
   {
      for (j = 1; j < ARRAYSIZE - 1; j++)
      {
         fscanf(jumbleIFP, "%c", &jumble[i][j]);
      }
      fscanf(jumbleIFP, "%c", &garbage);
   }

   return;
}


/*********************************************
 * PrintJumble()                             *
 *  inputs:                                  *
 *    char jumble[][ARRAYSIZE] - the array   *
 *            that holds the word search     *
 *                                           *
 *  outputs: prints jumble array to stdout   *
 *                                           *
 *  Prints the entire jumble array to stdout *
 *********************************************/
void PrintJumble(char jumble[][ARRAYSIZE])
{
    int i, j;
 
    /* loop through and print each char. don't forget the newlines! */
    for (i = 0; i < ARRAYSIZE - 1; i++)
    {
       for (j = 0; j < ARRAYSIZE - 1; j++)
       {
          printf("%c", jumble[i][j]);
       }
       printf("\n");
    }

    return;
}   


/*********************************************
 * OpenForWriting()                          *
 *  inputs:                                  *
 *    char * file - filename to open         *
 *                                           *
 *  outputs:                                 *
 *    returns a FILE * to the file opened    *
 *                                           *
 *  Opens a file with the filename passed in,*
 * returns the fileptr to that file.         *
 *  Note: This may seem like a somewhat      *
 *   pointless function, but I made it to be *
 *   consistent, since I wrote a functio for *
 *   OpenForReading(), and it only makes     *
 *   sense to have one for writing, too.     *
 *********************************************/
FILE * OpenForWriting(char * file)
{
   return fopen(file, "w");
}


/*********************************************
 * AllocateWORDMem()                         *
 *  inputs:                                  *
 *    int quantity - # of words to allocate  *
 *       space for                           *
 *                                           *
 *  outputs:                                 *
 *    returns a WORD * <-- array of WORDs    *
 *                                           *
 *  Allocates space (and makes sure that     *
 * space was allocated successfully) for     *
 * quantity number of words, and returns the *
 * pointer to the space allocated.           *
 *********************************************/
WORD * AllocateWORDMem (int quantity)
{
   WORD * array;

   array = (WORD*)malloc(sizeof(WORD) * quantity);
 
   /* make sure you still have memory */
   if (array == NULL)
   {
      fprintf(stderr, "ERROR! Out of memory.\n\n");
      exit(-3);
   }

   return array;
}


/*********************************************
 * FillFindArray()                           *
 *  inputs:                                  *
 *    FILE * ifp - filePtr to the file that  *
 *      has the list of words to find        *
 *    WORD * findMe - array of WORDs         *
 *    int numWords - size of findMe          *
 *                                           *
 *  outputs:                                 *
 *    intializes the findMe array to contain *
 *   what's in the file pted to by ifp, and  *
 *   clears all other fields in that WORD    *
 *   struct                                  *
 *                                           *
 *  For every word in the findMe array, it   *
 * reads in a word from the file pointed to  *
 * by ifp and initalizes its startRow and    *
 * startCol to be 0, as well as its direction*
 * to be UNKNOWN.                            *
 *********************************************/
void FillFindArray(FILE * ifp, WORD * findMe, int numWords)
{
   int i;

   /* for every word, scan in the string and clear its members */
   for (i = 0; i < numWords; i++)
   {
      fscanf(ifp, "%s", findMe[i].word);
      
      /* clear the rest of the struct */ 
      findMe[i].startRow = 0;
      findMe[i].startCol = 0;
      findMe[i].direction = DEFAULT;
   }
}


/*********************************************
 * GetNumWords()                             *
 *  inputs:                                  *
 *    FILE * ifp - pointer to the input file *
 *      of words to find                     *
 *                                           *
 *  outputs:                                 *
 *    returns the number of words in the file*
 *                                           *
 *  This function just counts the number of  *
 * words in the file.  NOTE that it reads up *
 * until the end of the file, and the filePtr*
 * must be rewound if you'd like to deal with*
 * it anymore.                               *
 *********************************************/
int GetNumWords(FILE * ifp)
{
   char temp[MAXWORDLEN];
   int numWords = 0;

   /* fscanf returns EOF at the end of the file, 
      so continue until then */
   while (fscanf(ifp, "%s", temp) != EOF)
   {
      numWords++;
   }

   return numWords;
}


/*********************************************
 * FindWords()                               *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - the    *
 *      array that holds the wordsearch chars*
 *    WORD * findMe - the array of words to  *
 *      search for                           *
 *    int numWords - size of findMe array    *
 *                                           *
 *  outputs:                                 *
 *    modifies the findMe array to contain   *
 *   where each findMe[i]'s word was found,  *
 *   as in its row, col, and direction.      *
 *                                           *
 *  This function loops through each word in *
 *  the findMe array and searches for the    *
 *  word in 4 directions:                    *
 *   1. horizontally forward  (left)         *
 *   2. horizontally backward (right)        *
 *   3. vertically forward    (down)         *
 *   4. vertically backward   (up)           *
 *  Each of these checks are calls to a      *
 *  different recursive function.  If none of*
 *  these calls find the word, the word is   *
 *  left to be 'unfound'.                    *
 *********************************************/
void FindWords(char jumbleArray[][ARRAYSIZE], WORD * findMe, int numWords)
{
   int i;

   /* for each word, search until you find it! */
   for (i = 0; i < numWords; i++)
   {
      /* try horizontal forward (left) */
      if (HorizontalFwdSearch(jumbleArray, 0, 0, findMe[i].word,
                              &findMe[i].startRow, &findMe[i].startCol,
			      strlen(findMe[i].word)) == TRUE)
      {
	 findMe[i].direction = RIGHT;
      }
      else
      {         	 
	 /* try horizontal backward (right) */
	 if (HorizontalBwdSearch(jumbleArray, ARRAYSIZE - 1,
				 ARRAYSIZE - 1, findMe[i].word,
                                 &findMe[i].startRow, &findMe[i].startCol,
				 strlen(findMe[i].word)) == TRUE)
	 {
	    findMe[i].direction = LEFT;
	 }
	 else
	 {
	    /* try vertical forward (down) */
            if (VerticalFwdSearch(jumbleArray, 0, 
                                  0, findMe[i].word,
                                  &findMe[i].startRow, &findMe[i].startCol,
                                  strlen(findMe[i].word)) == TRUE)
            {
               findMe[i].direction = DOWN;
            }
            else
            { 
	       /* try vertical backward (up) */
               if (VerticalBwdSearch(jumbleArray, ARRAYSIZE - 1,
				     ARRAYSIZE - 1, findMe[i].word, 
				     &findMe[i].startRow, &findMe[i].startCol,
				     strlen(findMe[i].word)) == TRUE)
               {
                  findMe[i].direction = UP;
               }
               else
               {
		  /* that word isn't in there!!!! */
	          findMe[i].direction = DEFAULT;
               }
	    }
	 }
      }            
   }
}


/*********************************************
 * HorizontalFwdSearch()                     *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * horizontal ordering of a string (RIGHT)   *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain 0 so the recursion               *
 *  can start at the top left corner of      *
 *  the array and work its way forward. The  *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int HorizontalFwdSearch(char jumbleArray[][ARRAYSIZE], int row, 
                     int col, char * word, 
                     int * foundRow, int * foundCol, int origLen)
{
   /* no more letters -- FOUND! */
   if (strlen(word) == 0)
   {
      *foundRow = row;
      *foundCol = col - origLen;
      return TRUE;
   }
   
   /* this letter doesn't match, return */
   if ((strlen(word) < origLen) && (jumbleArray[row][col] != word[0]))
   {  
      return FALSE;
   }
   
   /* at the end of the col, add the row */
   if (col > ARRAYSIZE - strlen(word))
   {
      row++;
      col = 0;
   }
   
   /* no more rows, didnt find it. */
   if (row == ARRAYSIZE - 1)
   {
      return FALSE;
   } 
  
   /* one character match...*/
   if (jumbleArray[row][col] == word[0])
   {
    
      /* try a search on the rest of the string */
      if (HorizontalFwdSearch(jumbleArray, row, col+1, word+1,
                              foundRow, foundCol, origLen) == TRUE)
      {
	 /* if it worked on the rest of the leters, it worked. */
         return TRUE;
      }
   }

   /* keep searching. */
   return HorizontalFwdSearch(jumbleArray, row, col+1, word, 
                              foundRow, foundCol, origLen);
}


/*********************************************
 * VerticalFwdSearch()                       *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * vertical ordering of a string (DOWN)      *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain 0 so the recursion               *
 *  can start at the top left corner of      *
 *  the array and work its way forward. The  *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int VerticalFwdSearch(char jumbleArray[][ARRAYSIZE], int row,
                     int col, char * word,
                     int * foundRow, int * foundCol, int origLen)
{
   /* found it!! */
   if (strlen(word) == 0)
   {
      *foundRow = row - origLen;
      *foundCol = col;
      return TRUE;
   }

   /* this letter doesn't match, so no match. */
   if ((strlen(word) < origLen) && (jumbleArray[row][col] != word[0]))
   {
      return FALSE;
   }

   /* at the end of the row, add the column */
   if (row > ARRAYSIZE - strlen(word))
   {
      col++;
      row = 0;
   }

   /* no more columns! didn't find it */
   if (col == ARRAYSIZE - 1)
   {
      return FALSE;
   }

   /* if this character matches....*/
   if (jumbleArray[row][col] == word[0])
   {

      /* try the rest of the string on the next spot. */
      if (VerticalFwdSearch(jumbleArray, row+1, col, word+1,
                              foundRow, foundCol, origLen) == TRUE)
      {
	 /* found it !*/
         return TRUE;
      }
   }

   /* move on in the array. */
   return VerticalFwdSearch(jumbleArray, row+1, col, word,
                              foundRow, foundCol, origLen);
}

/*********************************************
 * HorizontalBwdSearch()                     *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * horizontal ordering of a string (LEFT)    *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain ARRAYSIZE - 1 so the recursion   *
 *  can start at the bottom right corner of  *
 *  the array and work its way backward. The *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int HorizontalBwdSearch(char jumbleArray[][ARRAYSIZE], int row, 
                     int col, char * word, int * foundRow,
                     int * foundCol, int origLen)
{
   /* found it!! */
   if (strlen(word) == 0)
   {
      *foundRow = row;
      *foundCol = col + origLen;
      return TRUE;
   }
  
   /* this char won't match, give up */
   if ((strlen(word) < origLen) && (jumbleArray[row][col] != word[0]))
   {  
      return FALSE;
   }

   /* watch the borders! go to the previous row and start again */
   if (col < strlen(word))
   {
      row--;
      col = ARRAYSIZE-1;
   }
   
   /* no more rows, you didn't find it */
   if (row == 0)
   {
      return FALSE;
   }  

   /* one character matches...*/
   if (jumbleArray[row][col] == word[0])
   {
      /* try to search for the rest of the string */
      if (HorizontalBwdSearch(jumbleArray, row, col-1, word+1, 
			      foundRow, foundCol, origLen) == TRUE)
      {
	 /* found it!!! */
         return TRUE;
      }
   }

   /* keep searching */
   return HorizontalBwdSearch(jumbleArray, row, col-1, word, 
			      foundRow, foundCol, origLen);
}

 
/*********************************************
 * VerticalBwdSearch()                       *
 *  inputs:                                  *
 *    char jumbleArray[][ARRAYSIZE] - holds  *
 *      the word search characters           *
 *    int row - row coordinate to look at    *
 *    int col - col coordinate to look at    *
 *    char * word - word to search for       *
 *    int * foundRow - ptr to save row #     *
 *    int * foundCol - ptr to save col #     *
 *    int origLen - original length of the   *
 *      original string                      *
 *                                           *
 *  outputs:                                 *
 *    returns TRUE if the word was found;    *
 *     *foundRow and *foundCol contain where *
 *     the word was found                    *
 *                                           *
 *  This function recursively searches the   *
 * vertical ordering of a string (UP)        *
 * USAGE:  When calling this function, the   *
 *  initial row and col parameters should    *
 *  contain ARRAYSIZE-1 so the recursion     *
 *  can start at the bottom right corner of  *
 *  the array and work its way forward. The  *
 *  origLen is just the strlen() of the word *
 *  you're trying to find.                   *
 *********************************************/
int VerticalBwdSearch(char jumbleArray[][ARRAYSIZE], int row,
                     int col, char * word, int * foundRow,
                     int * foundCol, int origLen)
{
   /* found it!!! */
   if (strlen(word) == 0)
   {
      *foundRow = row + origLen;
      *foundCol = col;
      return TRUE;
   }

   /* this char didn't match, give up. */
   if ((strlen(word) < origLen) && (jumbleArray[row][col] != word[0]))
   {
      return FALSE;
   }

   /* watch borders...get a new column and reset the row */
   if (row < strlen(word))
   {
      col--;
      row = ARRAYSIZE-1;
   }

   /* you're done and you didn't find it. */
   if (col == 0)
   {
      return FALSE;
   }
   
   /* one char matches...*/
   if (jumbleArray[row][col] == word[0])
   {
 
      /* search for the rest of hte word */
      if (VerticalBwdSearch(jumbleArray, row-1, col, word+1,
			    foundRow, foundCol, origLen) == TRUE)
      {
	 /* found it!!! */
         return TRUE;
      }
   }

   /* keep searching */
   return VerticalBwdSearch(jumbleArray, row-1, col, word,
                              foundRow, foundCol, origLen);
}

 
/*********************************************
 * PrintAnswers()                            *
 *  inputs:                                  *
 *    char * file - filename to print to     *
 *    WORD * words - word array              *
 *    int numWords - size of word array      *
 *                                           *
 *  outputs:                                 *
 *    prints the results of the wordsearch   *
 *   to the file opened named filename       *
 *                                           *
 *  This function loops through the words    *
 * array and prints the results of the search*
 * for that word.  If the word was found, the*
 * direction of its orientation, as well as  *
 * its [row][col] coordinates are printed.   *
 * If the word was not found, the [row][col] *
 * printed is 0 and 0, and the orientation is*
 * "Not Present".                            *
 *********************************************/
void PrintAnswers(char * file, WORD * words, int numWords)
{
   FILE * ofp;
   int i, j;

   ofp = OpenForWriting(file);

   /* print the header */
   fprintf(ofp, "\n%s %15s %4s %4s %11s\n",
	   "Word", "", "Row", "Col", "Direction");
  
   fprintf(ofp, "------------------------------------------\n");

   /* print the results for every word */
   for (i = 0; i < numWords; i++) 
   {
      fprintf(ofp, "%s", words[i].word);
      
      for (j = 0; j < NUM_SPACES - strlen(words[i].word); j++)
      {
	 fprintf(ofp, " ");
      }
      fprintf(ofp, " %2d   %2d ", words[i].startRow, words[i].startCol);
      
      fprintf(ofp, "  %s\n", GetDirectionStr(words[i].direction));
   }
   
   fprintf(ofp, "\n");
   fclose(ofp);
}


/*********************************************
 * GetDirectionStr()                         *
 *  inputs:                                  *    
 *      char d - direction of a string       *
 *                                           *
 *  outputs:                                 *
 *    returns the string that char direction *
 *     represents                            *
 *                                           *
 *  This function takes the direction as one *
 * character and returns a printable string  *
 * of what that character represents.  Ex:   *
 *    if (d == 'U')                          *
 *     GetDirectionStr() returns "Up"        *
 *    if (d == '?')                          *
 *     GetDirectionStr() returns "Not Present"*
 *********************************************/
char * GetDirectionStr(char d)
{
   switch(d)
   {
      case UP:
	 return UP_STRING;
	 break;
      case DOWN:
	 return DOWN_STRING;
	 break;
      case LEFT:
	 return LEFT_STRING;
	 break;
      case RIGHT:
	 return RIGHT_STRING;
	 break;
      case DEFAULT:
	 return DEFAULT_STRING;
	 break;
      default:
	 return UNKNOWN_STRING;
   }
   
   return UNKNOWN_STRING;
}
   

/*********************************************
 * PrintWordsToFind()                        *
 *  inputs:                                  *
 *    WORD * findMe - word array             *
 *    int numWords - size of word array      *
 *                                           *
 *  outputs:                                 *
 *    prints a list of words to find to      *
 *    stdout                                 *
 *                                           *
 *  This function loops through the words    *
 * array and prints its .word field to stdout*
 *********************************************/
void PrintWordsToFind(WORD * findMe, int numWords)
{
   int i;


   for (i = 0; i < numWords; i++)
   {
      printf("\t%s\n", findMe[i].word);
   }

}
      

/*********************************************
 * PrintGreeting()                           *
 *  inputs: (none)                           *    
 *                                           *
 *  outputs:                                 *
 *    prints a greeting to the user (stdout) *
 *                                           *
 *  This function greets the user.           *
 *********************************************/
void PrintGreeting(void)
{
    printf("\n\tWelcome to Natalie's WordSearcher!!!\n\n");

}

Generated by GNU enscript 1.6.1.