Hi, I am trying to write a code to make run 3D wordseach. Basically what I have to do is, I have to read a file that has a grid of wordsearch in it. For example,
3 3 3
abc
def
ghi
jkl
mno
pqr
stu
vwx
yza
something like this. One chunk is one level, horizontal line is row, vertical line is column. Numbers at the top indicate numbers of level, row, column. So 'a' would be in Level 1, Row 1, Column 1. Letter 'o' would be in Level 2, Row 2, Column 3.
Then I also have to call another file that contains a list of words that I am looking for. For examiple,
3
abc
aei
cny
like this. words are connected in a straight line, so it can be across the grid too.
So far, I can read the file and print the grid and the word list, and the following code will do so.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
FILE *grid_open;
grid_open = fopen(argv[1], "r");
int L, R, C, r, c;
char new_line;
fscanf(grid_open, "%d %d %d", &R, &C, &L);
printf("# of Rows: %d # of Columns: %d # of Levels: %d ", R, C, L);
printf("\n");
fscanf(grid_open, "%c", &new_line); //new line goes in here
char grid [R][C][L];
int l;
for(l = 1; l <= L; l++)
{
for(r = 1; r <= R; r++)
{
for(c = 1; c <= C; c++)
{
fscanf(grid_open, "%c", &grid[r][c][l]);
}
fscanf(grid_open, "%c", &new_line);
}
fscanf(grid_open, "%c", &new_line);
}
for(l = 1; l <= L; l++)
{
for(r = 1; r != R + 1; r++)
{
for(c = 1; c <= C; c++)
{
printf("%c", grid[r][c][l]);
}
printf("\n");
}
printf("\n");
}
FILE *word_open;
word_open = fopen(argv[2], "r");
int W, w;
fscanf(word_open, "%d", &W);
printf("Words left:\n");
printf("\n");
fscanf(word_open, "%c", &new_line); //new line goes in here
char word[W][C];
for(w = 0; w != W; w++)
{
fscanf(word_open, "%s", &word[w][0]);
puts(word[w]);
}
return 0;
}
Now I have to find the words in the grid one by one, but I am not sure how I would do that. What I am thinking is, go through the grid one by one, and if I find the first letter from the word, I store that letter into string, then look for another letter, and if I find the second letter from the word, add that letter after the first letter.
So for example, if I am trying to find the word "abc", I start with 'a' in the grid. Since it is the first letter of the word, I store that into string. Then I check the next character from the grid, which is 'b', and since it's the second letter of the word, I use [strcat] to add 'b' into 'a', so the string now contains "ab". Then finally, do the same with 'c', so the string now contains "abc", which is the word I am looking for.
The problem is, I do not know how to do that . . .
Can anyone PLEASE help me with this problem?
It would be a huge help for me.
Thank you.