Code:
#define DIM 15 /* size of array*/
#define WORDNUM 18 /* number of words in the list */
void printwords();
int compareWords(int value, char array[]);
typedef struct {
char structword[15];
int row;
int column;
} WORD;
WORD wordList[WORDNUM];
void readWords(void);
char puzzle[DIM][DIM]={ {'E','U','N','I','T','N','O','C','+','+','+','+','+','+','+'},
{'X','+','+','+','+','+','+','+','+','+','+','+','A','+','+'},
{'I','+','+','+','+','+','+','T','+','P','R','I','N','T','F'},
{'T','+','+','+','+','+','+','U','+','+','+','+','S','+','L'},
{'+','W','H','I','T','E','S','P','A','C','E','+','I','+','O'},
{'T','+','+','+','+','+','+','+','+','+','+','+','+','+','A'}, {'E','+','N','O','I','S','S','E','R','P','X','E','+','+','T'},
{'S','+','+','+','+','+','+','+','+','+','+','+','+','+','+'},
{'T','N','E','M','E','R','C','N','I','+','P','O','W','E','R'},
{'+','+','+','A','+','+','+','+','+','+','+','+','+','+','E'},
{'E','R','O','C','+','I','+','E','L','B','U','O','D','+','T'},
{'+','+','+','R','+','N','+','+','+','+','+','L','L','U','N'},
{'+','+','+','O','+','T','L','U','A','F','E','D','+','+','I'},
{'+','+','+','S','+','+','+','+','+','+','+','+','+','+','O'},
{'+','+','+','+','+','S','Y','M','B','O','L','I','C','+','P'}};
int main()
{
char array[DIM] ;
int x, y, w;
int type=0;
int c = 0, r = 0;
readWords();
int numb =0;
int count;
for (count = 0; count< WORDNUM; count++)
{
printf("%s \n",wordList[count].structword);
}
/* horizontal comparison*/
for (x=1; x<=15; x++)
{
for (y=1; y<=15; y++)
{
w=0;
/*printf("%c",puzzle[x][y]);*/
if (puzzle[x][y]!= '+')
{
r = y;
while (puzzle[x][y]!= '+')
{
array[w] = puzzle[x][y];
printf("%c",array[w]);
w++;
y++;
}
array[w] = '\0';
result = compareWords(r, array);
for (numb=0; numb < WORDNUM; numb++)
{
if (strcmp(wordList[numb].structword , array)
== 0)
{
wordList[numb].row = result;
wordList[numb].column = x;
}
}
y = y + strlen(array);
/*printf("-%d-",y);*/
}
else
continue;
}
}
/* vertical comparison*/
for (y=1; y<=15;y++)
{
for (x=1; x<=15; x++)
{
w=0;
if (puzzle[x][y]!= '+')
{
c = x;
while (puzzle[x][y]!='+')
{
array[w] = puzzle[x][y];
w++;
x++;
}
array[w] = '\0';
result = compareWords(c, array);
for (numb=0; numb < WORDNUM; numb++)
{
if (strcmp(wordList[numb].structword , array) == 0)
{
wordList[numb].row = y;
wordList[numb].column = type;
}
}
x = x + strlen(array);
}
}
}
printwords ();
return 0;
}
void printwords()
{
int linecount;
for (linecount=0; linecount < WORDNUM; linecount++)
{
printf("\n %s (%d,%d)",wordList[linecount].structword,wordList[linecount].row,wordList[linecount].column);
}
}
int compareWords(int value, char array[])
{
int linenum;
int l = strlen(array);
for( linenum = 0; linenum < WORDNUM; linenum++)
{
if ( wordList[linenum].structword[0] == array[0])
return value;
else if ( wordList[linenum].structword[0] == array[l - 1] )
return -1 * (value + l);
else
{
++linenum;
}
}
return 0;
}
void readWords(void)
{
int count = 0;
char word[15];
while (scanf("%s", word)!=EOF)
{
strcpy(wordList[count].structword, word);
count++;
}
}