In: Computer Science
//================readFile.h================
/*
* readFile.h
*
* Created on: 31-Oct-2020
* Author: abc
*/
#ifndef READFILE_H_
#define READFILE_H_
#include <stdio.h>
#include <stdlib.h>
//________________________
int readFile(char fileName[],char arr[][51]){
FILE *fp;
fp=fopen(fileName,"r");
if(fp==NULL){
printf("Unable to open
file\n");
return -1;
}
int count=0;
while(fscanf(fp,"%[^\n]\n ",arr[count])!=EOF){
count++;
}
fclose(fp);
return count;
}
//_______________________
void printArray(char arr[][51],int size){
for(int i=0;i<size;i++){
printf("%s\n",arr[i]);
}
}
#endif /* READFILE_H_ */
//====end of readFile.h====================
//=====findWord.h=================
/*
* findWord.h
*
* Created on: 31-Oct-2020
* Author: niran
*/
#ifndef FINDWORD_H_
#define FINDWORD_H_
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#define COL 51
#define ROW 50
//_________________________
int south(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(row<ROW){
if(word[i]!=arr[row][col])
return 0;
row++;
}else{
break;
}
}
if(i==len){
*endRow=row-1;
*endCol=col;
return 1;
}
else
return 0;
}
//_________________________
int southWest(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(row<ROW &&
col>=0){
if(word[i]!=arr[row][col])
return 0;
row++;
col--;
}else{
break;
}
}
if(i==len){
*endRow=row-1;
*endCol=col+1;
return 1;
}
else
return 0;
}
//__________________________
int southEast(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(row<ROW &&
col<COL-1){
if(word[i]!=arr[row][col])
return 0;
row++;
col++;
}else{
break;
}
}
if(i==len){
*endRow=row-1;
*endCol=col-11;
return 1;
}
else
return 0;
}
//_________________________
int north(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(row>=0){
if(word[i]!=arr[row][col])
return 0;
row--;
}else{
break;
}
}
if(i==len){
*endRow=row+1;
*endCol=col;
return 1;
}
else
return 0;
}
//_______________________
int northWest(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(row>=0 &&
col>=0){
if(word[i]!=arr[row][col])
return 0;
row--;
col--;
}else{
break;
}
}
if(i==len){
*endRow=row+1;
*endCol=col+1;
return 1;
}
else
return 0;
}
//________________________
int northEast(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(row>=0 &&
col<COL-1){
if(word[i]!=arr[row][col])
return 0;
row--;
col++;
}else{
break;
}
}
if(i==len){
*endRow=row+1;
*endCol=col-1;
return 1;
}
else
return 0;
}
//________________________
int east(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(col<COL-1){
if(word[i]!=arr[row][col])
return 0;
col++;
}else{
break;
}
}
if(i==len){
*endRow=row;
*endCol=col-1;
return 1;
}
else
return 0;
}
//__________________________
int west(char arr[][COL],char word[COL],int row,int col,int
*endRow,int *endCol){
int len=strlen(word);
int i;
for(i=0;i<len;i++){
if(col>=0){
if(word[i]!=arr[row][col])
return 0;
col--;
}else{
break;
}
}
if(i==len){
*endRow=row;
*endCol=col+1;
return 1;
}
else
return 0;
}
//_________________________
void findWord(char puzzle[][COL],char word[COL],FILE *fp){
int startRow,startCol,endRow,endCol;
int found=0;
for(int i=0;i<ROW;i++){
startRow=i;
for(int j=0;j<COL-1;j++){
startCol=j;
found=south(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):S\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):S\n",word,startRow,startCol,endRow,endCol);
}
found=north(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):N\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):N\n",word,startRow,startCol,endRow,endCol);
}
found=east(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):E\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):E\n",word,startRow,startCol,endRow,endCol);
}
found=west(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):W\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):W\n",word,startRow,startCol,endRow,endCol);
}
found=northWest(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):NW\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):NW\n",word,startRow,startCol,endRow,endCol);
}
found=northEast(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):NE\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):NE\n",word,startRow,startCol,endRow,endCol);
}
found=southWest(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):SW\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):SW\n",word,startRow,startCol,endRow,endCol);
}
found=southEast(puzzle,word,i,j,&endRow,&endCol);
if(found==1){
printf("%s found @ (%d, %d)-(%d,
%d):SE\n",word,startRow,startCol,endRow,endCol);
fprintf(fp,"%s found @ (%d, %d)-(%d,
%d):SE\n",word,startRow,startCol,endRow,endCol);
}
}
}
}
#endif /* FINDWORD_H_ */
//==========end of findWord.h==============
//=====testMain.cpp==========================
/*
* testMain.c
*
* Created on: 31-Oct-2020
* Author: abc
*/
#include <stdio.h>
#include "readFile.h"
#include "findWord.h"
int main(){
char puzzle[50][51];
char word[150][51];
int n=readFile("WordSearchPuzzle.txt",puzzle);
int m=readFile("WordList.txt",word);
FILE *fp=fopen("result.txt","w");
if(fp==NULL){
printf("Unable to open
file!\n");
return 0;
}
for(int i=0;i<m;i++){
findWord(puzzle,word[i],fp);
}
fclose(fp);
return 0;
}
//====end of testMAin.c=======================
//result.txt
ABACUS found @ (42, 10)-(47, 5):SE
ALIEN found @ (12, 39)-(8, 35):NW
BANSHEE found @ (21, 4)-(21, 10):E
BASILISK found @ (7, 2)-(0, 2):N
BAT found @ (5, 1)-(7, 1):S
BAT found @ (8, 24)-(6, 22):NW
BAT found @ (13, 22)-(15, 20):SW
BAT found @ (36, 20)-(38, 20):S
BAT found @ (39, 2)-(37, 2):N
BEAST found @ (36, 20)-(32, 20):N
BEHEMOTH found @ (7, 17)-(0, 24):NE
BLOODYMARY found @ (14, 32)-(14, 41):E
BOGEYMAN found @ (42, 4)-(42, 11):E
BONE found @ (3, 16)-(0, 13):NW
BOO found @ (20, 3)-(18, 3):N
BOO found @ (29, 41)-(31, 41):S
CADAVER found @ (35, 15)-(41, 11):SE
CANDYMAN found @ (42, 0)-(49, -3):SE
CEMETERY found @ (8, 15)-(15, 15):S
CHUD found @ (16, 45)-(13, 45):N
CHUPACABRA found @ (28, 11)-(37, 10):SE
COBWEB found @ (39, 27)-(44, 27):S
COFFIN found @ (5, 21)-(10, 16):SW
CORPSE found @ (45, 9)-(45, 4):W
CRONENBERG found @ (32, 48)-(41, 48):S
CRYPT found @ (27, 43)-(23, 47):NE
CTHULHU found @ (39, 22)-(45, 22):S
DARK found @ (15, 17)-(18, 14):SW
DARK found @ (29, 38)-(26, 41):NE
DARKNESS found @ (15, 17)-(22, 10):SW
DEATH found @ (37, 6)-(37, 10):E
DEMENTOR found @ (30, 47)-(30, 40):W
DEMON found @ (31, 10)-(35, 4):SE
DEVIL found @ (13, 47)-(17, 47):S
DUNGEON found @ (18, 27)-(24, 21):SW
EERIE found @ (14, 4)-(18, 0):SW
ELDONNA found @ (7, 32)-(7, 26):W
ELF found @ (5, 46)-(3, 48):NE
ELF found @ (9, 22)-(11, 14):SE
ELF found @ (12, 20)-(12, 18):W
ELF found @ (14, 22)-(14, 24):E
FENRIR found @ (14, 24)-(9, 19):NW
FRIGHT found @ (16, 25)-(11, 25):N
GHOST found @ (18, 40)-(18, 36):W
GHOUL found @ (30, 49)-(26, 45):NW
GOBLIN found @ (5, 20)-(0, 25):NE
GORE found @ (6, 36)-(6, 39):E
GRAVEYARD found @ (11, 5)-(3, 5):N
HALLOWEEN found @ (15, 16)-(7, 24):NE
HAUNTED found @ (40, 15)-(34, 9):NW
HOMUNCULUS found @ (27, 21)-(36, 12):SW
HYOSUBE found @ (33, 8)-(27, 8):N
IMP found @ (38, 22)-(38, 24):E
JACKALOPE found @ (46, 22)-(46, 30):E
JACKOLANTERN found @ (11, 33)-(0, 33):N
JINN found @ (42, 12)-(45, 5):SE
KRAKEN found @ (46, 31)-(46, 36):E
KRAMPUS found @ (40, 0)-(34, 6):NE
KRUEGER found @ (17, 14)-(17, 20):E
LAMIA found @ (47, 39)-(43, 39):N
LILIN found @ (20, 45)-(16, 49):NE
LLORONA found @ (0, 11)-(6, 7):SE
LYCAN found @ (32, 37)-(32, 41):E
MACABRE found @ (8, 10)-(14, 10):S
MARA found @ (9, 30)-(12, 30):S
MEDUSA found @ (23, 23)-(28, 18):SE
MIDNIGHT found @ (11, 29)-(4, 22):NW
MORBID found @ (16, 31)-(21, 31):S
MUMMY found @ (0, 3)-(4, -3):SE
NIGHT found @ (8, 26)-(4, 22):NW
NIGHT found @ (16, 43)-(20, 43):S
NIGHT found @ (32, 13)-(36, 7):SE
NIGHTMARE found @ (32, 13)-(40, 11):SE
NYMPH found @ (5, 22)-(9, 18):SW
OCTOBER found @ (0, 12)-(6, 6):SW
OGRE found @ (9, 23)-(12, 20):SW
PHANTASM found @ (29, 37)-(22, 37):N
PHANTOM found @ (44, 38)-(44, 32):W
POLTERGEIST found @ (34, 35)-(24, 45):NE
PUCKWUDGIE found @ (21, 37)-(21, 28):W
QAREEN found @ (14, 29)-(19, 24):SW
REAPER found @ (21, 43)-(21, 38):W
RIP found @ (21, 13)-(19, 15):NE
RIP found @ (31, 27)-(33, 27):S
RIP found @ (33, 17)-(35, 9):SE
RIP found @ (40, 26)-(38, 24):NW
ROACH found @ (29, 27)-(29, 23):W
ROBOT found @ (4, 48)-(0, 44):NW
SCREAM found @ (38, 44)-(33, 39):NW
SKULL found @ (26, 49)-(22, 49):N
SLENDERMAN found @ (35, 9)-(26, 0):NW
SPELL found @ (13, 20)-(9, 24):NE
SPIDER found @ (14, 26)-(9, 26):N
SPOOKY found @ (20, 1)-(15, 6):NE
TARANTULA found @ (5, 13)-(13, 13):S
THESTRAL found @ (13, 16)-(6, 23):NE
TOMBSTONE found @ (0, 49)-(8, 41):SW
TREAT found @ (48, 16)-(48, 20):E
TRICK found @ (45, 0)-(49, -6):SE
TROLL found @ (32, 21)-(36, 21):S
UNICORN found @ (6, 48)-(0, 42):NW
VAMPIRE found @ (36, 27)-(30, 27):N
WAMPUS found @ (6, 25)-(6, 30):E
WARLOCK found @ (32, 14)-(26, 20):NE
WENDIGO found @ (22, 38)-(22, 44):E
WEREWOLVE found @ (27, 17)-(27, 9):W
WHITELADY found @ (20, 48)-(12, 40):NW
WRAITH found @ (23, 14)-(28, 9):SE
WYVERN found @ (37, 21)-(42, 21):S
XIAO found @ (40, 44)-(40, 41):W
XINDHI found @ (2, 39)-(7, 34):SW
YOKAI found @ (43, 16)-(47, 12):SW
YUREI found @ (44, 43)-(44, 39):W
ZOMBIE found @ (0, 19)-(0, 14):W