SlideShare a Scribd company logo
1 of 7
Download to read offline
#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;
}

More Related Content

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

1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
arishmarketing21
 

Similar to #include ctype.h #include stdio.h #include string.hstati.pdf (20)

Ds
DsDs
Ds
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
 
Array
ArrayArray
Array
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Ada file
Ada fileAda file
Ada file
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Pnno
PnnoPnno
Pnno
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Lab 6 (1)
Lab 6 (1)Lab 6 (1)
Lab 6 (1)
 
week-20x
week-20xweek-20x
week-20x
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
1D Array
1D Array1D Array
1D Array
 

More from apleather

The below stated are the Challenges and business requirements faced .pdf
The below stated are the Challenges and business requirements faced .pdfThe below stated are the Challenges and business requirements faced .pdf
The below stated are the Challenges and business requirements faced .pdf
apleather
 
Form1.csusing System; using System.Collections.Generic; using .pdf
Form1.csusing System; using System.Collections.Generic; using .pdfForm1.csusing System; using System.Collections.Generic; using .pdf
Form1.csusing System; using System.Collections.Generic; using .pdf
apleather
 
Influenza A The recently raised awareness of th.pdf
                     Influenza A  The recently raised awareness of th.pdf                     Influenza A  The recently raised awareness of th.pdf
Influenza A The recently raised awareness of th.pdf
apleather
 
Data warehousing has quickly evolved into a unique and popular busin.pdf
Data warehousing has quickly evolved into a unique and popular busin.pdfData warehousing has quickly evolved into a unique and popular busin.pdf
Data warehousing has quickly evolved into a unique and popular busin.pdf
apleather
 

More from apleather (20)

pH = -log[H+] pH = -log(0.5) pH = 0.301 .pdf
                     pH = -log[H+]  pH = -log(0.5)  pH = 0.301        .pdf                     pH = -log[H+]  pH = -log(0.5)  pH = 0.301        .pdf
pH = -log[H+] pH = -log(0.5) pH = 0.301 .pdf
 
The universality of the genetic code provides strong support for the.pdf
The universality of the genetic code provides strong support for the.pdfThe universality of the genetic code provides strong support for the.pdf
The universality of the genetic code provides strong support for the.pdf
 
The pus is consists of neutrophils which target the pathogen. The pa.pdf
The pus is consists of neutrophils which target the pathogen. The pa.pdfThe pus is consists of neutrophils which target the pathogen. The pa.pdf
The pus is consists of neutrophils which target the pathogen. The pa.pdf
 
The least soluble gas is N2 because it is inert and cannot form any .pdf
The least soluble gas is N2 because it is inert and cannot form any .pdfThe least soluble gas is N2 because it is inert and cannot form any .pdf
The least soluble gas is N2 because it is inert and cannot form any .pdf
 
The below stated are the Challenges and business requirements faced .pdf
The below stated are the Challenges and business requirements faced .pdfThe below stated are the Challenges and business requirements faced .pdf
The below stated are the Challenges and business requirements faced .pdf
 
Sometimes CEO can also hold the position of Chairperson, but in some.pdf
Sometimes CEO can also hold the position of Chairperson, but in some.pdfSometimes CEO can also hold the position of Chairperson, but in some.pdf
Sometimes CEO can also hold the position of Chairperson, but in some.pdf
 
SolutionInorder traversal It will consider left child root, and .pdf
SolutionInorder traversal It will consider left child root, and .pdfSolutionInorder traversal It will consider left child root, and .pdf
SolutionInorder traversal It will consider left child root, and .pdf
 
In a side the length of sides are same. use this property iterativel.pdf
In a side the length of sides are same. use this property iterativel.pdfIn a side the length of sides are same. use this property iterativel.pdf
In a side the length of sides are same. use this property iterativel.pdf
 
Elements or steps of acceptance testingSolutionElements or ste.pdf
Elements or steps of acceptance testingSolutionElements or ste.pdfElements or steps of acceptance testingSolutionElements or ste.pdf
Elements or steps of acceptance testingSolutionElements or ste.pdf
 
Long term capital = Common stock + shareholders equity + Retained ea.pdf
Long term capital = Common stock + shareholders equity + Retained ea.pdfLong term capital = Common stock + shareholders equity + Retained ea.pdf
Long term capital = Common stock + shareholders equity + Retained ea.pdf
 
Limbs evolve so frequently that it is impossible to determine otherw.pdf
Limbs evolve so frequently that it is impossible to determine otherw.pdfLimbs evolve so frequently that it is impossible to determine otherw.pdf
Limbs evolve so frequently that it is impossible to determine otherw.pdf
 
hmmSolutionhmm.pdf
hmmSolutionhmm.pdfhmmSolutionhmm.pdf
hmmSolutionhmm.pdf
 
Form1.csusing System; using System.Collections.Generic; using .pdf
Form1.csusing System; using System.Collections.Generic; using .pdfForm1.csusing System; using System.Collections.Generic; using .pdf
Form1.csusing System; using System.Collections.Generic; using .pdf
 
Influenza A The recently raised awareness of th.pdf
                     Influenza A  The recently raised awareness of th.pdf                     Influenza A  The recently raised awareness of th.pdf
Influenza A The recently raised awareness of th.pdf
 
I believe its named Methyl iodide. You wouldn.pdf
                     I believe its named Methyl iodide. You wouldn.pdf                     I believe its named Methyl iodide. You wouldn.pdf
I believe its named Methyl iodide. You wouldn.pdf
 
Data warehousing has quickly evolved into a unique and popular busin.pdf
Data warehousing has quickly evolved into a unique and popular busin.pdfData warehousing has quickly evolved into a unique and popular busin.pdf
Data warehousing has quickly evolved into a unique and popular busin.pdf
 
balready postedSolutionbalready posted.pdf
balready postedSolutionbalready posted.pdfbalready postedSolutionbalready posted.pdf
balready postedSolutionbalready posted.pdf
 
Assume the graph is strongly connected.SolutionAssume the grap.pdf
Assume the graph is strongly connected.SolutionAssume the grap.pdfAssume the graph is strongly connected.SolutionAssume the grap.pdf
Assume the graph is strongly connected.SolutionAssume the grap.pdf
 
Half life is the time in which the compound reduc.pdf
                     Half life is the time in which the compound reduc.pdf                     Half life is the time in which the compound reduc.pdf
Half life is the time in which the compound reduc.pdf
 
from the given reaction, order NO3=1 overall orde.pdf
                     from the given reaction, order NO3=1 overall orde.pdf                     from the given reaction, order NO3=1 overall orde.pdf
from the given reaction, order NO3=1 overall orde.pdf
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 

#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_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);
  • 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; }