I'm tasked to code a word search solver.
In this program, the user will be the one to decide the size of the grid, the letters in the grid, the number of words to search for, and the words to search for.
Here is the farthest I've gone. Here, I planned to search for the word immediately after it has been entered.
#include <stdio.h>
#include <string.h>
int main()
{
int rows, cols; //for the size of grid
int grid_row=0, grid_col=0; // rows/columns of grid
int numsrch; //# of words to look for
printf("Enter grid size: ");
scanf("%d %d", &rows, &cols);
cols+=1; //# of letters per line
char grid[rows][cols]; //array for grid
printf("Enter grid: \n");
for(grid_row=0; grid_row < rows; grid_row++) //where letters in grid are entered
{
scanf("%s", grid[grid_row]);
strlwr(grid[grid_row]);
}
printf("Number of words to look for: ");
scanf("%d", &numsrch);
int gridlookcol=0, gridlookrow=0;
int ltrsrch=0; //to be used later when looking for a letter in the word that's being searched for
char srch[cols]; //array for the word to be looked for
char found[cols]; //array for matched letters
for(grid_row=0; grid_row<numsrch; grid_row++)
{
strcpy(found,"");
ltrsrch=0;
scanf("%s", srch);
strlwr(srch);
for(gridlookrow=0;gridlookrow<rows;gridlookrow++)
{
if(strstr(grid[gridlookrow], srch) || strstr(grid[gridlookrow],strrev(srch)))
{
printf("%s begins at row %d\n", srch, gridlookrow + 1);
break;
}
}
}
return 0;
}
So, with the code above, I've sort of succeeded in matching words horizontally to the right. But I still have 7 directions left (up, down, left, up-right, up-left, down-left,down-right). Will I have to code a for loop for each direction?