0

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?

JC8
  • 43
  • 7
  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Mar 22 '16 at 11:50
  • 1
    In other words, please try to at least show some effort, and try something yourself. If you have tried a couple of times, you can come here with the solution that worked best, and ask us about it. If you just want to learn about pointers, [then find a good beginners book or tutorial](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Mar 22 '16 at 11:52
  • I'm sorry. I know I shouldn't be asking this question here but I really am desperate. @JoachimPileborg – JC8 Mar 22 '16 at 11:54
  • 2
    The question is valid, but you need to show some thought or example of thigns you've tried. Perhaps even break it into a smaller question. Why not start with a fixed sized grid, say 5x5 (not too large), with fixed letters and think about how you would store that in PHP that allows you to search against a wordlist ... get that sorted and you've made a great start. – Egg Mar 22 '16 at 11:56
  • 1
    @Egg umm.. 'PHP'? – Martin James Mar 22 '16 at 11:59
  • Just to clarify.. you have been trying for two days to input an integer 'size' from the user? – Martin James Mar 22 '16 at 12:01
  • Sorry, I meant "variable" ... mind on other things :) – Egg Mar 22 '16 at 12:01
  • 1
    @Egg if word association changes 'variable' to 'PHP' in your head, you should seek professional advice: It is possible that PHP has infected your brain and compromised your immune system:) – Martin James Mar 22 '16 at 12:05
  • @MartinJames I already succeeded in having the user input everything. What I'm having a problem with is looking for the words. What I've been trying to do for the past 2 days is to code a loop for each direction. Does that make sense? – JC8 Mar 22 '16 at 12:05
  • 1
    I have no idea what algorithm you plan to use. Are you taking each word, looking for all ocurrances of the first letter and then checking in all 8 directions? Show your code and data. – Martin James Mar 22 '16 at 12:13
  • Ok. Good. You've tried something. But you need to mention 1) Your inputs 2) The output 3) The expected output . Please don't post a wall of code without telling us what the problem is. Edit your post to include them and I'll cast a vote to reopen the question. Cheers! – Spikatrix Mar 22 '16 at 12:35
  • @CoolGuy added an example input and output. Thanks! – JC8 Mar 22 '16 at 12:56
  • Ok. Great! Question will be reopened shortly. And `for(grid_row=0; grid_row <= rows; grid_row++)` →`for(grid_row=0; grid_row < rows; grid_row++)`. Also, note that [`strlwr` is a non-standard function](http://stackoverflow.com/q/26327812/3049655) and [`gets` is dangerous](http://stackoverflow.com/q/1694036/3049655) – Spikatrix Mar 22 '16 at 13:00
  • And there is no need for semicolons(`;`) after `if(…){…}` – Spikatrix Mar 22 '16 at 13:05
  • @CoolGuy Thank you! But, if I changed the for loop as you said, the number of rows in the grid won't be as many as the number of rows the user wants the grid to have. Also, I used strlwr so that when I compare two characters, they will be exactly the same. – JC8 Mar 22 '16 at 13:05
  • "_the number of rows in the grid won't be as many as the number of rows the user wants the grid to have_" - If the user enters 2 for `rows`, `for(grid_row=0; grid_row < rows; grid_row++)` will loop 2 times, filling in 2 rows. Your version fills in 3 rows, writing into invalid memory locations past the array, invoking undefined behavior. – Spikatrix Mar 22 '16 at 13:09
  • ^^ I, too, applied a reopen vote. maybe, in return, you could run your app under a debuugger? :) – Martin James Mar 22 '16 at 13:11
  • @CoolGuy I see. Thanks – JC8 Mar 22 '16 at 13:17
  • Same thing here too: `for(gridlookrow=0;gridlookrow<=rows;gridlookrow++)` – Spikatrix Mar 22 '16 at 13:20
  • Another way to do it: `for(int i = 0; i < rows; i++) if(strstr(grid[i], srch)) { printf("'%s' is horizontally oriented in '%s'.\n", srch, grid[i]); }` – Spikatrix Mar 22 '16 at 13:27
  • @CoolGuy Wow!! Am I correct in thinking that the if statement you showed will replace the for loop that checks each line of the grid per letter? I'll try it out, but thanks!! – JC8 Mar 22 '16 at 13:34
  • That code can replace `for(gridlookrow=0;gridlookrow – Spikatrix Mar 22 '16 at 13:44
  • See [`strstr`](http://www.cplusplus.com/reference/cstring/strstr/) – Spikatrix Mar 22 '16 at 13:45
  • BTW, if you take a paper and pencil and write the values of the variables in each iteration, you'll be able to figure out what's wrong. And always indent your code properly, use whitespace around operators to increase readability. – Spikatrix Mar 22 '16 at 13:51
  • By a quick look, I think you need `strcpy(found, ""); ltrsrch = 0;` just before `for(gridlookcol=0; gridlookcol – Spikatrix Mar 22 '16 at 13:53
  • @CoolGuy Thanks! Now the program can match a word in the grid that's oriented horizontally to the right! :) Will improve on your other notes. – JC8 Mar 22 '16 at 13:55
  • Hmm. Half of your question doesn't make sense now that you've edited the code… – Spikatrix Mar 22 '16 at 13:58
  • @CoolGuy But by the time it reaches that point, strcpy(found, ""); ltrsrch = 0; would have already been done as it happens right before for(gridlookrow=0;gridlookrow – JC8 Mar 22 '16 at 14:00
  • But it won't happen after the inner loop executes `for(gridlookcol=0; gridlookcol – Spikatrix Mar 22 '16 at 14:03
  • Consider deleting this question and reposting a new, fresh one. This has now become a mess with many comments. – Spikatrix Mar 22 '16 at 14:04
  • Oh, and you may want to change the title as well! :-) – Spikatrix Mar 22 '16 at 14:05
  • @CoolGuy That actually did happen. I see your point now. That's the reason why even if the word I'm looking for is oriented vertically, the program sees it as horizontal; because found doesn't "refresh" when the search is already in another row. Is this correct? – JC8 Mar 22 '16 at 14:09
  • Yeah. You got it! :-) Hmm. The time for reopening seems to be a bit longer than I expected… – Spikatrix Mar 22 '16 at 14:12
  • @CoolGuy I've been running some test inputs. Turns out strstr won't work if I'm looking for a word that's spelled backwards on the grid. Does it really work like that or am I doing something wrong here. The code I'm using is the same as the one above. – JC8 Mar 22 '16 at 14:36
  • You'll have to reverse `srch` before using `strstr`. Something like `char rev[cols] = {'\0'}; for(int i = 0, j = strlen(srch) - 1; j >= 0; j--, i++) rev[i] = srch[j]; if(strstr(grid[gridlookrow], srch) || strstr(grid[gridlookrow], rev)) printf("%s begins at row %d and is oriented horizontally. \n", srch, gridlookrow);` Warning: Untested code ↑ – Spikatrix Mar 22 '16 at 15:53
  • @CoolGuy was able to match already.Using `if(strstr(grid[gridlookrow], srch) || strstr(grid[gridlookrow],strrev(srch))) { printf("%s begins at row %d\n", srch, gridlookrow + 1); break; } ` – JC8 Mar 22 '16 at 16:01
  • Yes. That'll do as well. But note that `strrev` is non-standard which means that it is available on some systems only. This would reduce the portability of your code. – Spikatrix Mar 22 '16 at 16:04
  • Also, seeing that you need to search horizontally, vertically and diagonally, I suggest you ditch `strstr` and use loops. – Spikatrix Mar 22 '16 at 16:05
  • @CoolGuy In that case, will looking for the word immediately after it has been entered be advisable? or will I have to store all the entries first in another 2D array? – JC8 Mar 22 '16 at 16:08
  • I'd suggest reading over all the data into the 2D array, and after that, loop through the array, checking all directions. – Spikatrix Mar 22 '16 at 16:10
  • @CoolGuy So, in short, I'd have to revert to what I was originally doing? – JC8 Mar 22 '16 at 16:15
  • Yes… I'd suggest splitting each task into functions like `horizontalCheck`, `verticalCheck` etc – Spikatrix Mar 22 '16 at 16:18
  • @JC8 `Turns out strstr won't work if I'm looking for a word that's spelled backwards on the grid` - reverse the letters in the word and then do the compare – anita2R Mar 22 '16 at 22:24

0 Answers0