Hey, if anyone would be into checking out the code and giving me some pointers I'll post my code below. Also, if anyone would like to write the same program differently, or has written the same program in a non-similar way then i'd love to see how they went about doing things.
Code: Select all
#include <stdio.h>
#define MAXROW 51
#define MAXCOL 51
int countA(char *cp);
int main (void)
{
/* VARIABLE DECLARATIONS */
int idx;
int end;
int totalCols;
int totalRows;
int cCol;
int cRow;
int totalLetters;
char wordToFind[30];
char crossword[MAXROW][MAXCOL];
char crosswordFile[] = "c:/crossword.txt";
char answerFile[] = "c:/answerfile.txt";
FILE *fp;
/* VARIABLE INITILIZATION */
totalCols = 0;
totalRows = 0;
cCol = 0;
cRow = 0;
end = 0;
/* READ CROSSWORD PUZZLE */
fp = fopen(crosswordFile, "r");
if (fp != NULL)
{
while (!feof(fp))
{
fgets(crossword[cRow], MAXCOL, fp);
cRow++;
}
fclose(fp);
}
else
{
fprintf(stderr, "\nUnable to read crossword file\n");
}
/* TALLY TOTAL COLS & ROWS & WORDS */
for (idx=0;crossword[0][idx]!=NULL;idx++);
totalCols = idx-1; // the newline column will not be counted when tallying number of cols
totalRows = cRow;
/* GET WORD TO FIND */
while (!end)
{
printf("\nWord To Find (Ctrl+C to quit): ");
if (scanf("%s",&wordToFind))
{
char *cp = wordToFind;
totalLetters= countA(wordToFind);
fp = fopen(answerFile, "a");
for (cRow=0;cRow<totalRows;cRow++)
{
for (cCol=0;cCol<totalCols;cCol++)
{
// Check for vertical matches going down
idx=0;
while (*(cp+idx)==crossword[cRow+idx][cCol] && idx<totalLetters &&
totalRows-idx>=totalLetters-idx) { idx++; }
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Down\n",wordToFind,cRow,cCol);
printf("Vert-Down (%d,%d)\n",cRow,cCol);
}
idx=0;
while (*(cp+idx)==crossword[cRow-idx][cCol] && idx<totalLetters &&
totalRows-idx>=totalLetters-idx) { idx++; }
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Up\n",wordToFind,cRow,cCol);
printf("Vert-Up (%d,%d)\n",cRow,cCol);
}
// Check for horizontal matches going right
idx=0;
while (*(cp+idx)==crossword[cRow][cCol+idx] && idx<totalLetters &&
totalCols-idx>=totalLetters-idx) { idx++;}
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Right\n",wordToFind,cRow,cCol);
printf("Horiz-Right (%d,%d)\n",cRow,cCol);
}
// Check for horizontal matches going left
idx=0;
while (*(cp+idx)==crossword[cRow][cCol-idx] && idx<totalLetters &&
totalCols-idx>=totalLetters-idx) { idx++; }
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Left\n",wordToFind,cRow,cCol);
printf("Horiz-Left (%d,%d)\n",cRow,cCol);
}
// Check for diagonal matches going down and right
idx=0;
while (*(cp+idx)==crossword[cRow+idx][cCol+idx] && idx<totalLetters &&
totalCols-idx>=totalLetters-idx) { idx++; }
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Down Right\n",wordToFind,cRow,cCol);
printf("Diag going down and right (%d,%d)\n",cRow,cCol);
}
// Check for diagonal matches going down and left
idx=0;
while (*(cp+idx)==crossword[cRow+idx][cCol-idx] && idx<totalLetters &&
totalCols-idx>=totalLetters-idx) { idx++; }
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Down Up\n",wordToFind,cRow,cCol);
printf("Diag going down and left (%d,%d)\n",cRow,cCol);
}
// Check for diagonal matches going up and right
idx=0;
while (*(cp+idx)==crossword[cRow-idx][cCol+idx] && idx<totalLetters &&
totalCols-idx>=totalLetters-idx) { idx++; }
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Up Right\n",wordToFind,cRow,cCol);
printf("Diag going up and right (%d,%d)\n",cRow,cCol);
}
// Check for diagonal matches going up and left
idx=0;
while (*(cp+idx)==crossword[cRow-idx][cCol-idx] && idx<totalLetters &&
totalCols-idx>=totalLetters-idx) { idx++; }
if (idx==totalLetters) {
fprintf(fp,"%s (%d, %d) Up Left\n",wordToFind,cRow,cCol);
printf("Diag going up and left (%d,%d)\n",cRow,cCol);
}
} // End column loop
} // End row loop
fclose(fp);
} // End valid input check
else
{
printf("Invalid input entered.\n");
end = 1;
}
} // End while !end loop
return 0;
}
int countA(char *cp)
{
for(int idx=0;*(cp+idx)!=NULL;idx++);
return idx;
}