# Finding Words in a array[size][size]

• 03-07-2006
Finding Words in a array[size][size]
Hi there! I need some help to find words in a array[size][size]
Just like the game soup of letter. It has to find words in vertical, horizontal, diagonal and random positions.
Example of random:

EDIT:
___W
____O__TO
_____RD___FI
___________ND

The problem is that i can find horizontal, vertical and diagonal but not random ones. Please help me!!
• 03-07-2006
siliconian
Hi...
if u write your code, it will be better for helping...
Thanks...
• 03-07-2006
dwks
Do you know what words you're looking for?

I would suggest using a recursive function.

Post your code, as suggested.
• 03-07-2006
Yes i do know what words im looking for. U tought of recursion also, the problem is that i know well how to work with it.
• 03-07-2006
dwks
I would use a recursive function that looks in all directions for the words you're looking for. You would call the function on all elements of the array.
• 03-07-2006
I've tought in that also, the question is. Imagine that you find 3 correct letters and find a dead end. but in another direccion that you haven't choose there is the rest??
• 03-07-2006
oh sorry, here's the code. This one finds in all directions except for the random

Code:

```     do     {     i=0;     j=0;     x=pos_x;     y=pos_y;     encontrou=0;     nletras=0;     do{         encontrou=0;              if(sopa[x][y]==palavras[pos_palavra].palavra[i]){             palavras[pos_palavra].ponteiro[i]=&sopa[x][y];               i++;               nletras++;                         encontrou=1;                       switch(vez){                     case 0: x--; break;        /* Cima */                     case 1: {x--; y++; }break; /* Diagonal Cima Direita */                     case 2: y++; break;        /* Direita */                     case 3: {x++; y++; }break; /* Diagonal Baixo Direita */                     case 4: x++; break;        /* Baixo */                     case 5: {x++; y--; }break; /* Diagonal Baixo Esquerda*/                                case 6: y--; break;        /* Esquerda */                     case 7: {x--; y--; }break; /* Diagonal Cima Esquerda */                 }                        }                   if(nletras==strlen(palavras[pos_palavra].palavra)-1){             palavras[pos_palavra].encontrou=1;             printf("Encontrou a palavra %s\n", palavras[pos_palavra].palavra);         }                   }     while(encontrou==1 && palavras[pos_palavra].encontrou==0 && x>=0 && x<tamsopa && y>=0 && y<tamsopa);     vez++;     }     while (vez<8 && palavras[pos_palavra].encontrou==0);```
• 03-08-2006
dwks
Try something like this (add bounds checking):
Code:

```int find_word(int xp, int yp, int letter) {     if(letter == length) return 1;     if(array[xp][yp] != word[letter]) return 0;     if(find_word(xp+1, yp, letter+1)) return 1;     if(find_word(xp-1, yp, letter+1)) return 1;     /* ... */ }```
• 03-08-2006