# A basic Word Puzzle Algorithm

• 03-29-2007
m0ntana
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
• 03-29-2007
vart
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 */ }```
• 03-29-2007
m0ntana
Quote:

Originally Posted by vart
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..
• 03-29-2007
Machoscorpion
Quote:

Originally Posted by vart
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. ?
• 03-29-2007
KONI
Quote:

Originally Posted by Machoscorpion
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.
• 03-29-2007
m0ntana

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 :(