How would I create a grid for a word seach Assuming I have a
How would I create a grid for a word seach? Assuming I have all the words in the dictionary. Can someone please show me some code or explain how I could go about arranging those words into collumns or row for a word search. Where I can choose the grid size, ex) 3x3 or 7x7. This is in the programing language C
Thank you
Solution
#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
 struct Dictionary buildDictionary();
 struct WordGrid buildSearchGrid();
 char *reverStr();
 void printDictionary();
 void printWordGrid();
 void binaryWordSearch();
 void parseGrid();
 
 struct Dictionary{
 int wordLength;
 int numOfWords;
 char **ppWords;
 };
 
 struct WordGrid{
 int height;
 int width;
 char **ppGrid;
 };
 
 int main(){
 
 int i;
 int numOfCases;
 
 FILE *pFile = fopen(\"dictionary.txt\", \"r\");;
 
 if(pFile == NULL){
 printf(\"Can not open file.\");
 return 1;
 }
 
 struct Dictionary dictionary = buildDictionary(pFile, dictionary);
 
 scanf(\"%d\", &numOfCases);
 for( i = 0; i < numOfCases; i++){
 printf(\"Words found in grid #%d\ \", i+1);
 
 struct WordGrid wordGrid = buildSearchGrid(wordGrid);
 
 parseGrid(wordGrid, dictionary);
 }
 
 return 0;
 }
 
 char* reverStr(const char *str){
 
 size_t len = strlen(str);   
 char* revStr = (char*)malloc(len + 1);
 
 size_t dst, src;
 for (dst = len - 1, src = 0; src < len; src++, dst--) {
 revStr[dst] = str[src];
 }
 revStr[len] = \'\\0\';
 return revStr;
 }
 
 void parseGrid(struct WordGrid wordGrid, struct Dictionary dictionary){
 
 char *searchKey;
 int x, y;
 
 for( y = 0; y < wordGrid.height; y++){
 searchKey = wordGrid.ppGrid[y];
 binaryWordSearch(dictionary, searchKey);
 }
 
 for( y = 0; y < wordGrid.height; y++){
 searchKey = reverStr(wordGrid.ppGrid[y]);
 binaryWordSearch(dictionary, searchKey);
 }
 
 }
 
 void binaryWordSearch(struct Dictionary dictionary, char *searchKey){
 
 int first = 0;
 int last = dictionary.numOfWords - 1;
 int mid = (first + last) / 2;
 
 while(first <= last){
 if( strcmp(dictionary.ppWords[mid], searchKey) < 0 ){
    first = mid + 1;
 }
 else if( strcmp(dictionary.ppWords[mid], searchKey) == 0 ){
    printf(\"%s\ \", dictionary.ppWords[mid]);
    break;
 }
 else{
    last = mid - 1;
 }
 
 mid = (first + last) / 2;
 }
 
 if(first > last){
 }
 }
 
 struct Dictionary buildDictionary(FILE *pFile, struct Dictionary dictionary){
 
 dictionary.wordLength = 20;
 
 fscanf(pFile, \"%d\", &dictionary.numOfWords);
 
 dictionary.ppWords = malloc( dictionary.numOfWords * sizeof(char*));
 
 for(int i = 0; i < dictionary.numOfWords; i++){
 dictionary.ppWords[i] = malloc( dictionary.wordLength * sizeof(char));
 fscanf(pFile, \"%s\", dictionary.ppWords[i]);
 }
 
 return dictionary;
 }
 
 struct WordGrid buildSearchGrid(struct WordGrid wordGrid){
 
 scanf(\"%1d\", &wordGrid.height);
 scanf(\"%1d\", &wordGrid.width);
 
 wordGrid.ppGrid = malloc( wordGrid.height * sizeof(char*));
 
 for(int x = 0; x < wordGrid.width; x++){
 wordGrid.ppGrid[x] = malloc( wordGrid.width+1 * sizeof(char));
 scanf(\"%s\", wordGrid.ppGrid[x]);
 }
 
 return wordGrid;
 }
 
 void printDictionary(struct Dictionary dictionary){
 
 for(int i = 0; i < dictionary.numOfWords; i++){
 printf(\"%s\ \", dictionary.ppWords[i]);
 }
 }
 
 void printWordGrid(struct WordGrid wordGrid){
 
 printf(\"%d \", wordGrid.height);
 printf(\"%d\ \", wordGrid.width);
 
 for(int i = 0; i < wordGrid.height; i++){
 printf(\"%s\ \", wordGrid.ppGrid[i]);
 }
 }



