A basic Word Puzzle Algorithm

This is a discussion on A basic Word Puzzle Algorithm within the C Programming forums, part of the General Programming Boards category; Hi Everybody, I'm tryin to build and basic word puzzle algorithm, by saying "basic" I mean that It's not supposed ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    A basic Word Puzzle Algorithm

    Hi Everybody,

    I'm tryin to build and basic word puzzle algorithm, by saying "basic" I mean that It's not supposed to find word acrossly, it must search only forward-backward ,up and down...
    I must use Constant pointer arrays for all puzzle like the way below;

    Code:
    char* puzzle[LENGHT] =     {"jerome",
    			    "emorej",
    			    "rxxxxx",
    		            "oxxxxx",
    			    "mxxxxx",
    			    "exxxxx" };
    char* word[1]={"jerome"};
    When I first thought about it and made a couple of searches, I decided to use following way,
    I'm gonna use an temp[] char array and in the loops, I'll read all Lines (forward<->backward,up<->down) into the char array and I'll search the temp by usin string processing tehniques (strstr vs...)...

    What I cant do is, when I declared puzzle as string arrays not as one by one filling with chars, I couldn't be able to read strings one by one into "temp" array...

    For instance in a loop
    Code:
    temp[i]=*(puzzle+1);
    If I do this, it doesnt work anyway, or
    Code:
    strcopy(temp,*(puzzle+1));
    It copies all the string into the temp array, How can I read the strings char by char into the temp array, or is there any easier way to do this?
    Actually I'm lucky because, my alghorithm doesn't need to make a across search, that's why I'm gonna try to make it as easy-short as possible...

    thanks for now

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    const char* p = "test string";
    char temp[1024];
    int i;
    for(i=0;i < sizeof temp;i++)
    {
       /* read string char by char */
       temp[i] = p[i];
       if(p[i] == '\0') break; /* end of string found */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by vart View Post
    Code:
    const char* p = "test string";
    char temp[1024];
    int i;
    for(i=0;i < sizeof temp;i++)
    {
       /* read string char by char */
       temp[i] = p[i];
       if(p[i] == '\0') break; /* end of string found */
    }
    Thanks bro,
    For a long time I'been doin' same thing but everytime I try to call temp[0](to take first char) and while doing this I'm using %s instead of %c, I think I'm getting sick ...
    anyway after this I'm gonna get rest...thanks alot again..

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Quote Originally Posted by vart View Post
    Code:
       if(p[i] == '\0') break; /* end of string found */
    Why do you wanne do this.. ?

    you could do away with this by running the loop (sizeof temp-1) times. and i don't see you appending '\0' to temp anyway. !
    or am I missing something. ?

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Machoscorpion View Post
    Why do you wanne do this.. ?

    you could do away with this by running the loop (sizeof temp-1) times. and i don't see you appending '\0' to temp anyway. !
    or am I missing something. ?
    You wanna do this because tmp is of size 1024 bytes, but you only want to copy as many bytes/characters as there are in the string pointed at. Even if you'd say 1024-1, it's still way too much.

    And yes, he's appending \0 to tmp, since he checks and brakes only after he copied the character, so the last character copied will be the \0, after which he breaks.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    What about this,

    I created such a algorithm that works fine for my purposes, but one thing

    Code:
    char* puzzle[LENGHT] =     {"jerome",
    			    "emorej",
    			    "rxxxxx",
    		            "oxxxxx",
    			    "mxxxxx",
    			    "exxxxx" };
    char* word[1]={"jerome"};
    here is my declarations then I'm goona search "jerome" in puzzle,
    to do that;
    Code:
    for(i=0;i<LENGHT;i++){
    	for(j=0;j<5;j++){
    		strcpy(temp,*(puzzle+i));
    		start=strstr(temp,*(words+j));
    		if(start!=NULL){
    			printf("the Word \"%s\" is Found at %d.Line LEFT to RIGHT\n",words[j],i);
    This part was to search from left to right and in the other part I reverse the puzzle lines one by one and check them, revers is like shown below;
    Code:
    for(k=0;k<LENGHT;k++){          //x is equal to 1 while declaring
    		temp2[k]=temp[LENGHT-x];
    		x++;}
    		x=1;
    for instance ABCD->DCBA and research from them, if I'm looking for DCBA I may reach the result and printed it as right to left.

    And as a entire code it works, the point is what kind of algorithm may help me to make a Up and Down search like
    ABCAB
    BCARA
    RHARH

    suppose, I'm looking for "BAH" or "HAB"...

    For now I'm thinkin of it, but I couldt get any solution yet

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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