An interesting challenge --> A word search program

This is a discussion on An interesting challenge --> A word search program within the C Programming forums, part of the General Programming Boards category; I'm attempting to create a program that can go through a wordsearch that can pick out horizontal and vertical words. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    An interesting challenge --> A word search program

    I'm attempting to create a program that can go through a wordsearch that can pick out horizontal and vertical words. There are 20 words to be found, and they can have a maximum string length of 16 characters. The puzzeboard itself is 16x16 array and is composed of characters only. I was thinking of doing something like this:

    Code:
    #include <stdio.h>
    
    /*Maximum size for word search*/
    #define MAXSEARCH 16
    #define MAXWORDS  20
    
    /*Function prototypes*/
    void horizontal(char letters [MAXSEARCH][MAXSEARCH]);
    void vertical(char letters [MAXSEARCH][MAXSEARCH]);
    
    
    
    char wordsearch [MAXSEARCH][MAXSEARCH]={{'G','R','N','L','R','S','Y','S','T','E','M','E','E','O','M','R'},
                                            {'O','C','O','M','P','U','T','E','R','E','H','I','A','I','C','U'},
                                            {'R','A','I','M','P','R','O','G','R','A','M','A','N','R','R','R'},
                                            {'Q','M','E','M','O','R','Y','A','N','T','C','R','N','T','T','M'},
                                            {'L','A','O','N','E','T','W','O','R','K','R','O','H','H','E','U'},
                                            {'G','T','R','Y','S','T','R','I','N','G','I','A','E','G','Q','E'},
                                            {'R','R','R','N','E','A','N','Y','L','Y','I','L','E','E','U','R'},
                                            {'T','R','P','T','A','R','E','C','O','S','S','G','I','T','A','A'},
                                            {'R','T','P','L','A','R','N','A','G','O','M','E','R','B','T','U'},
                                            {'E','H','H','I','T','A','G','L','I','K','L','B','S','R','I','R'},
                                            {'N','E','Y','T','T','Y','I','C','C','M','C','R','M','I','O','O'},
                                            {'Y','O','S','R','A','H','N','U','U','G','R','A','E','D','N','A'},
                                            {'P','R','I','Y','O','R','E','L','C','U','U','C','A','G','S','R'},
                                            {'C','Y','C','G','E','K','E','U','R','S','S','B','A','E','L','S'},
                                            {'C','O','S','N','R','E','R','S','O','U','R','R','T','P','R','B'},
                                            {'C','N','P','O','C','N','R','M','R','U','A','I','G','A','S','O'}};
    
    char words [MAXWORDS][MAXSEARCH]= {{'T','H','E','O','R','Y'},
                                       {'S','T','R','I','N','G'},
                                       {'A','R','R','A','Y'},
                                       {'A','P','P','L','E'},
                                       {'E','N','G','I','N','E','E','R'},
                                       {'C','O','M','P','U','T','E','R'},
                                       {'P','H','Y','S','I','C','S'},
                                       {'C','A','L','C','U','L','U','S'},
                                       {'A','L','G','E','B','R','A'},
                                       {'T','I','G','E','R'},
                                       {'B','R','I','D','G','E'},
                                       {'N','E','T','W','O','R','K'},
                                       {'P','R','O','G','R','A','M'},
                                       {'H','O','U','S','E'},
                                       {'E','Q','U','A','T','I','O','N'},
                                       {'M','E','M','O','R','Y'},
                                       {'S','L','E','E','P'},
                                       {'L','O','G','I','C'},
                                       {'S','Y','S','T','E','M'},
                                       {'P','I','A','N','O'}};
    
    /*Main Function*/
    
    int main() {
    	
        int horizontalwords [MAXSEARCH*MAXSEARCH];
        int verticalwords   [MAXSEARCH*MAXSEARCH];
        int i=0;
        int j=0;
    
        printf("Copied by Rows");
        
        for (i=0;i<MAXSEARCH; i++) {
           for  (j=0;j<MAXSEARCH; j++) {
            
                 horizontalwords[i]=wordsearch[i][j];
    	     printf ("%s\n", horizontalwords[i]);		     
            }
        }
        
        printf("Copied by Columns");
        
        for (i=0;i<MAXSEARCH; i++) {
           for  (j=0;j<MAXSEARCH; j++) {
            
                 verticalwords[i]=wordsearch[j][i];
    	     printf ("%s\n", verticalwords[i]);		     
            }
        }
        
        
         
    return 0;
    
    }
    That basically copies the array by columns and by rows into two separate matrices. The matrix with the columns copied into it will have all the vertical words. The matrix with the rows copied into it wil have all the horizontal words. My plan was to have two functions, one for the copied rows, and one for the copied columns, and for the functions to go thorugh the arrays and find the words, but im not to sure how to implement that. Any tips or sugestions would be appreciated!

    -desi

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an idea:
    Code:
    for each word to find
        for each cell in the grid
            if this is the letter matches the first letter of the word we're looking for
                check across for each remaining letter in the word to see if it's a match
                check down for each remaining letter in the word to see if it's a match
    There are ways to short-cut your word matching also, such as, is the word we're working on finding's length longer than the remaining letters in the row or column. You can also use the array's position as an argument to something like strcmp, at least for your horizontal checking.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You can also use the array's position as an argument to something like strcmp, at least for your horizontal checking.
    Not with his current code. The arrays aren't NULL-terminated.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Good catch, but I'm sure there's a null character somewhere in memory.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Probably.

    But he could use strncmp() instead, since they aren't NULL-terminated. Or he could just add NULLs.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Or he could just add NULLs.
    Or he could just use string literals to begin with an save himself a ton of trouble. I'd get tired typing so many commas and single quotes to build those arrays.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 04:58 AM
  2. Writing a Unix Search Program, some questions
    By Acolyte in forum C Programming
    Replies: 3
    Last Post: 09-23-2008, 01:53 AM
  3. Search Engine C++ program
    By Jaidan in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2007, 02:29 AM
  4. Simple search program
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 12-18-2004, 01:58 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21