Code:
#include <stdio.h>#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h> //Used for the'toupper' function
int main()
{
int a,X,Y,i,length;
char array[26][26], string[50];
/* Above defines the 26X26 array in which to fit the wordsearch
as well as a search string of length 50 (more than enough to search for words within the wordsearch) */
printf("WORDSEARCH SOLVER - PRESS CTRL-C TO QUIT\n\n");
FILE *wsearch=fopen("computerwordsearchdata.txt","r");
if (wsearch!=NULL) //Checking file eXists/opens correctlY
{
for (Y=0; Y<26 ;Y++)
{
for(X=0;X<26;X++)
{
fscanf(wsearch,"%c\n",&array[Y][X]); //Inputting each character into array
array[Y][X]=toupper(array[Y][X]);
}
}
fclose(wsearch);
for (Y=0;Y<26;Y++)
{
for (X=0;X<26;X++)
{
printf("%c",array[Y][X]); //Printing output to screen
}
printf("\n");
}
printf("\nPlease enter the word to be searched:\n");
fgets(string,50,stdin); //Using the fgets function to read in the string from keyboard - prevents buffer overflow
for(i=0;string[i]!='\0';i++)
{
string[i]=toupper(string[i]); //Converts string all into uppercase to prevent errors
}
length=strlen(string);
for (Y=0;Y<26;Y++)
{
for(X=0;X<26;X++)
{
if(array[Y][X]==(string[0]))
{
printf("\nLetter 1 of %sis found at %d,%d\n",string,X,Y);
//Success in finding first letter of string - Now trY in other directions
if (array[--Y][X]==string[1])
{
//Success in finding in Y+ direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[--Y][X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in Y+ direction");
break;
}
else if (array[Y][++X]==string[1])
{
//Success in finding in Y+X+ direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[--Y][++X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in Y+X+ direction");
break;
}
else if (array[++Y][X]==string[1])
{
//Success in finding in X+ direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[Y][++X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in X+ direction");
break;
}
else if (array[++Y][X]==string[1])
{
//Success in finding in X+Y- direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[++Y][++X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in X+Y- direction");
break;
}
else if (array[Y][--X]==string[1])
{
//Success in finding in Y- direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[++Y][X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in Y- direction");
break;
}
else if (array[Y][--X]==string[1])
{
//Success in finding in X-Y- direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[++Y][--X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in X-Y- direction");
break;
}
else if (array[--Y][X]==string[1])
{
//Success in finding in X- direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[Y][--X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in X- direction");
break;
}
else if (array[--Y][X]==string[1])
{
//Success in finding in X-Y+ direction
printf("Letter 2 of %sis found at %d,%d",string,X,Y);
for (a=2;a<length;a++)
{
if (array[--Y][--X]==string[a])
printf("\nLetter %d of %sis found at %d,%d",a+1,string,X,Y);
}
printf("\n\nWord found in X-Y+ direction");
break;
}
else
{
//If word is not found in any direction
printf("Word not found.");
break;
}
}
}
break;
}
} //End of file opening correctly process
else //If file does not open correctly
{
printf("File not found"); //Shows when file is not in folder
}
printf("\n");
return 0;
}