#include
#include
#include
static int read_data(char *buffer, size_t buflen);
static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy);
static int searchData_from(char arr[18][18], char *str, int len, int m, int n);
int main(void)
{
int m, n, len;
char arr[18][18], str[50];
printf("Welcome to word search puzzle CTRL-C TO QUIT  ");
FILE *fileData = fopen("dataSearch.txt", "r");
if (!fileData)
{
printf("Error occured in opening file ");
return 1;
}
for (n = 0; n < 18; n++)
{
for (m = 0; m < 18; m++)
{
fscanf(fileData, " %c", &arr[n][m]);
arr[n][m] = toupper(arr[n][m]);
}
}
fclose(fileData);
printf(" ");
for (m = 0; m < 18; m++)
printf("%-2d", m);
printf(" ______________________________________ ");
for (n = 0; n < 18; n++)
{
printf("%-2d|", n);
for (m = 0; m < 18; m++)
printf("%c ", arr[n][m]);
printf(" ");
}
while ((len = read_data(str, sizeof(str))) != EOF)
{
printf("Searching for: [%s] ", str);
int totalCount = 0;
for (n = 0; n < 18; n++)
{
for (m = 0; m < 18; m++)
{
if (arr[n][m] == (str[0]) && searchData_from(arr, str, len, m, n))
totalCount++;
}
}
printf("Found %s %d times ", str, totalCount);
}
printf(" ");
return 0;
}
// reading data from file
static int read_data(char *buffer, size_t buflen)
{
printf(" Please enter the word to be searched: ");
if (fgets(buffer, buflen, stdin) == 0)
return EOF;
size_t len = strlen(buffer);
if (buffer[len-1] == ' ')
buffer[--len] = '0';
if (len == 0)
return EOF;
for (size_t i = 0; i < len; i++)
buffer[i] = toupper(buffer[i]);
return len;
}
// searching data from array
static int searchData_from(char arr[18][18], char *str, int len, int m, int n)
{
struct yx { int dy; int dx; } puzzleDirection[] =
{
{ +1, 0 }, { -1, 0 }, { +1, +1 }, { -1, +1 },
{ 0, +1 }, { 0, -1 }, { -1, -1 }, { +1, -1 },
};
enum { num_directions = sizeof(puzzleDirection) / sizeof(puzzleDirection[0]) };
int totalCount = 0;
for (int i = 0; i < num_directions; i++)
{
if (direction_searching(arr, str, len, m, n, puzzleDirection[i].dx, puzzleDirection[i].dy))
totalCount++;
}
return totalCount;
}
// checking in given direction
static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy)
{
for (int i = 1; i < len; i++)
{
int x = m + i * dx;
int y = n + i * dy;
if (x < 0 || x >= 18 || y < 0 || y >= 18)
return 0;
if (arr[y][x] != str[i])
return 0;
}
printf("word Found %s starting at (%d,%d) to (%d,%d) ",
str, n, m, n + (len - 1) * dy, m + (len - 1) * dx);
/* checking words*/
char *pad = "";
for (int i = 0; i < len; i++)
{
int x = m + i * dx;
int y = n + i * dy;
printf("%s%c (%d,%d)", pad, arr[y][x], y, x);
pad = ", ";
}
putchar(' ');
return 1;
}
Solution
#include
#include
#include
static int read_data(char *buffer, size_t buflen);
static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy);
static int searchData_from(char arr[18][18], char *str, int len, int m, int n);
int main(void)
{
int m, n, len;
char arr[18][18], str[50];
printf("Welcome to word search puzzle CTRL-C TO QUIT  ");
FILE *fileData = fopen("dataSearch.txt", "r");
if (!fileData)
{
printf("Error occured in opening file ");
return 1;
}
for (n = 0; n < 18; n++)
{
for (m = 0; m < 18; m++)
{
fscanf(fileData, " %c", &arr[n][m]);
arr[n][m] = toupper(arr[n][m]);
}
}
fclose(fileData);
printf(" ");
for (m = 0; m < 18; m++)
printf("%-2d", m);
printf(" ______________________________________ ");
for (n = 0; n < 18; n++)
{
printf("%-2d|", n);
for (m = 0; m < 18; m++)
printf("%c ", arr[n][m]);
printf(" ");
}
while ((len = read_data(str, sizeof(str))) != EOF)
{
printf("Searching for: [%s] ", str);
int totalCount = 0;
for (n = 0; n < 18; n++)
{
for (m = 0; m < 18; m++)
{
if (arr[n][m] == (str[0]) && searchData_from(arr, str, len, m, n))
totalCount++;
}
}
printf("Found %s %d times ", str, totalCount);
}
printf(" ");
return 0;
}
// reading data from file
static int read_data(char *buffer, size_t buflen)
{
printf(" Please enter the word to be searched: ");
if (fgets(buffer, buflen, stdin) == 0)
return EOF;
size_t len = strlen(buffer);
if (buffer[len-1] == ' ')
buffer[--len] = '0';
if (len == 0)
return EOF;
for (size_t i = 0; i < len; i++)
buffer[i] = toupper(buffer[i]);
return len;
}
// searching data from array
static int searchData_from(char arr[18][18], char *str, int len, int m, int n)
{
struct yx { int dy; int dx; } puzzleDirection[] =
{
{ +1, 0 }, { -1, 0 }, { +1, +1 }, { -1, +1 },
{ 0, +1 }, { 0, -1 }, { -1, -1 }, { +1, -1 },
};
enum { num_directions = sizeof(puzzleDirection) / sizeof(puzzleDirection[0]) };
int totalCount = 0;
for (int i = 0; i < num_directions; i++)
{
if (direction_searching(arr, str, len, m, n, puzzleDirection[i].dx, puzzleDirection[i].dy))
totalCount++;
}
return totalCount;
}
// checking in given direction
static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy)
{
for (int i = 1; i < len; i++)
{
int x = m + i * dx;
int y = n + i * dy;
if (x < 0 || x >= 18 || y < 0 || y >= 18)
return 0;
if (arr[y][x] != str[i])
return 0;
}
printf("word Found %s starting at (%d,%d) to (%d,%d) ",
str, n, m, n + (len - 1) * dy, m + (len - 1) * dx);
/* checking words*/
char *pad = "";
for (int i = 0; i < len; i++)
{
int x = m + i * dx;
int y = n + i * dy;
printf("%s%c (%d,%d)", pad, arr[y][x], y, x);
pad = ", ";
}
putchar(' ');
return 1;
}

#include ctype.h #include stdio.h #include string.hstati.pdf

  • 1.
    #include #include #include static int read_data(char*buffer, size_t buflen); static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy); static int searchData_from(char arr[18][18], char *str, int len, int m, int n); int main(void) { int m, n, len; char arr[18][18], str[50]; printf("Welcome to word search puzzle CTRL-C TO QUIT "); FILE *fileData = fopen("dataSearch.txt", "r"); if (!fileData) { printf("Error occured in opening file "); return 1; } for (n = 0; n < 18; n++) { for (m = 0; m < 18; m++) { fscanf(fileData, " %c", &arr[n][m]); arr[n][m] = toupper(arr[n][m]); } } fclose(fileData); printf(" "); for (m = 0; m < 18; m++) printf("%-2d", m); printf(" ______________________________________ "); for (n = 0; n < 18; n++) { printf("%-2d|", n); for (m = 0; m < 18; m++) printf("%c ", arr[n][m]);
  • 2.
    printf(" "); } while ((len= read_data(str, sizeof(str))) != EOF) { printf("Searching for: [%s] ", str); int totalCount = 0; for (n = 0; n < 18; n++) { for (m = 0; m < 18; m++) { if (arr[n][m] == (str[0]) && searchData_from(arr, str, len, m, n)) totalCount++; } } printf("Found %s %d times ", str, totalCount); } printf(" "); return 0; } // reading data from file static int read_data(char *buffer, size_t buflen) { printf(" Please enter the word to be searched: "); if (fgets(buffer, buflen, stdin) == 0) return EOF; size_t len = strlen(buffer); if (buffer[len-1] == ' ') buffer[--len] = '0'; if (len == 0) return EOF; for (size_t i = 0; i < len; i++) buffer[i] = toupper(buffer[i]); return len; } // searching data from array static int searchData_from(char arr[18][18], char *str, int len, int m, int n)
  • 3.
    { struct yx {int dy; int dx; } puzzleDirection[] = { { +1, 0 }, { -1, 0 }, { +1, +1 }, { -1, +1 }, { 0, +1 }, { 0, -1 }, { -1, -1 }, { +1, -1 }, }; enum { num_directions = sizeof(puzzleDirection) / sizeof(puzzleDirection[0]) }; int totalCount = 0; for (int i = 0; i < num_directions; i++) { if (direction_searching(arr, str, len, m, n, puzzleDirection[i].dx, puzzleDirection[i].dy)) totalCount++; } return totalCount; } // checking in given direction static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy) { for (int i = 1; i < len; i++) { int x = m + i * dx; int y = n + i * dy; if (x < 0 || x >= 18 || y < 0 || y >= 18) return 0; if (arr[y][x] != str[i]) return 0; } printf("word Found %s starting at (%d,%d) to (%d,%d) ", str, n, m, n + (len - 1) * dy, m + (len - 1) * dx); /* checking words*/ char *pad = ""; for (int i = 0; i < len; i++) { int x = m + i * dx; int y = n + i * dy; printf("%s%c (%d,%d)", pad, arr[y][x], y, x);
  • 4.
    pad = ","; } putchar(' '); return 1; } Solution #include #include #include static int read_data(char *buffer, size_t buflen); static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy); static int searchData_from(char arr[18][18], char *str, int len, int m, int n); int main(void) { int m, n, len; char arr[18][18], str[50]; printf("Welcome to word search puzzle CTRL-C TO QUIT "); FILE *fileData = fopen("dataSearch.txt", "r"); if (!fileData) { printf("Error occured in opening file "); return 1; } for (n = 0; n < 18; n++) { for (m = 0; m < 18; m++) { fscanf(fileData, " %c", &arr[n][m]); arr[n][m] = toupper(arr[n][m]); } } fclose(fileData); printf(" "); for (m = 0; m < 18; m++)
  • 5.
    printf("%-2d", m); printf(" ______________________________________"); for (n = 0; n < 18; n++) { printf("%-2d|", n); for (m = 0; m < 18; m++) printf("%c ", arr[n][m]); printf(" "); } while ((len = read_data(str, sizeof(str))) != EOF) { printf("Searching for: [%s] ", str); int totalCount = 0; for (n = 0; n < 18; n++) { for (m = 0; m < 18; m++) { if (arr[n][m] == (str[0]) && searchData_from(arr, str, len, m, n)) totalCount++; } } printf("Found %s %d times ", str, totalCount); } printf(" "); return 0; } // reading data from file static int read_data(char *buffer, size_t buflen) { printf(" Please enter the word to be searched: "); if (fgets(buffer, buflen, stdin) == 0) return EOF; size_t len = strlen(buffer); if (buffer[len-1] == ' ') buffer[--len] = '0'; if (len == 0)
  • 6.
    return EOF; for (size_ti = 0; i < len; i++) buffer[i] = toupper(buffer[i]); return len; } // searching data from array static int searchData_from(char arr[18][18], char *str, int len, int m, int n) { struct yx { int dy; int dx; } puzzleDirection[] = { { +1, 0 }, { -1, 0 }, { +1, +1 }, { -1, +1 }, { 0, +1 }, { 0, -1 }, { -1, -1 }, { +1, -1 }, }; enum { num_directions = sizeof(puzzleDirection) / sizeof(puzzleDirection[0]) }; int totalCount = 0; for (int i = 0; i < num_directions; i++) { if (direction_searching(arr, str, len, m, n, puzzleDirection[i].dx, puzzleDirection[i].dy)) totalCount++; } return totalCount; } // checking in given direction static int direction_searching(char arr[18][18], char *str, int len, int m, int n, int dx, int dy) { for (int i = 1; i < len; i++) { int x = m + i * dx; int y = n + i * dy; if (x < 0 || x >= 18 || y < 0 || y >= 18) return 0; if (arr[y][x] != str[i]) return 0; } printf("word Found %s starting at (%d,%d) to (%d,%d) ", str, n, m, n + (len - 1) * dy, m + (len - 1) * dx);
  • 7.
    /* checking words*/ char*pad = ""; for (int i = 0; i < len; i++) { int x = m + i * dx; int y = n + i * dy; printf("%s%c (%d,%d)", pad, arr[y][x], y, x); pad = ", "; } putchar(' '); return 1; }