Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program problem, can someone help me please? We will implement a word search g

ID: 3806683 • Letter: C

Question

C program problem, can someone help me please?

We will implement a word search game for this problem. The file DataFile.txt contains a word search puzzle that is size 201 x 100 (201 lines and each line has 100 characters all characters are uppercase. This puzzle is made simple in that all words are hidden horizontally, and in forward order (i no words are written backwards, nor vertically nor diagonally) Although the words are all hidden in forward order, they may occur multiple times on any given line (see sample code execution) Since there are no lowercase characters in the puzzle, you should not worry about converting lowercase to uppercase or vice versa A sample portion of the puzzle is which is difficult enough to search a it is The words that are hidden in the puzzle are: PROGRAMMING, FPRINTF, FSCANF, DOUBLE MAIN, DOWHILE, FGETS, INT, IF, WHILE, ENGINEERING, RETURN, CHAR, FCLOSE ELECTRICAL, ECE, VOID, FLOAT, FOR

Explanation / Answer

#include<stdio.h>
#include <stdlib.h>
//To keep track of total number of times the word occurs in file
int totalTimes = 0;
//To search a word in a line from the file
int search(char line[], char word[])
{
int count1 = 0, count2 = 0, i, j, flag, x = 0, times = 0;
//To calculate length
while (line[count1] != '')
count1++;
while (word[count2] != '')
count2++;
//Loops till i is less than or equals to line length minus word length
for (i = 0; i <= count1 - count2; i++)
{
//Loops from i value to word plus i value
for (j = i; j < i + count2; j++)
{
//Initially flag is one
flag = 1;
//Checks lines j position is not equals to words j minus 1 position
if (line[j] != word[j - i])
{
//If true set the flag to zero
flag = 0;
break;
}//End of if
}//End of inner loop
//Checks if flag value is one increase the times counter by 1
if (flag == 1)
{
times++;
}
}//End of outer loop
//Checks if times is not zero then word found
if (times != 0)
//Add times with totalTimes
totalTimes += times;
//Return number of times word found in a line
return times;
}//End of function

//To read file
void readFile()
{
char line[100], word[20];
int res, lineNumber = 0;
//Creates a file pointer and opens file in read mode
FILE *file = fopen("DataFile.txt", "r");
//Checks if not found show error message
if (!file)
{
fprintf(stderr, "error: could not open text file ");
exit(0);
}//End of if
//Accept a word to search
printf(" Enter a word to search: ");
scanf("%s", word);
//Loops till end of file and extract a line from the file
while (fgets(line, sizeof(line), file))
{
//Increase the line counter
lineNumber++;
//Displays line contents
printf(" The line is: %s", line);
fflush(stdin);
//Calls the search method to count how many times word occurs in the line
res = search(line, word);
//Displays current line information
printf(" Found %s %d times on line %d", word, res, lineNumber);
}//End of file
//Displays how many times the word found in file
printf(" The string: "%s" was found a total of %d times in the puzzle", word, totalTimes);
//Close the file
fclose(file);
}//End of function

//Main function
int main()
{
char ch;
//Loops till user choice
do
{
//Read the file
readFile();
//Reset the totalTimes to zero for every execution
totalTimes = 0;
//Accepts user choice
printf(" Do you want to search another word (q to quit)?");
scanf("%c", &ch);
//Checks user choice if it is q then exit the program
if(ch == 'q' || ch == 'Q')
break;
}while(1);//End of while
}//End of main

DataFile.txt Contents:

ADFLKMAINASDFLKJMAINASDVOIDWERZXCVMAINWEPORIMXSADF
QWERPOIAXCWERVOIDWQMAINISDVVOIDQWERYUIZXCHJKERUIP

Output:

Enter a word to search: MAIN

The line is: ADFLKMAINASDFLKJMAINASDVOIDWERZXCVMAINWEPORIMXSADF

Found MAIN 3 times on line 1
The line is: QWERPOIAXCWERVOIDWQMAINISDVVOIDQWERYUIZXCHJKERUIP
Found MAIN 1 times on line 2
The string: "MAIN" was found a total of 4 times in the puzzle
Do you want to search another word (q to quit)?Y

Enter a word to search: VOID

The line is: ADFLKMAINASDFLKJMAINASDVOIDWERZXCVMAINWEPORIMXSADF

Found VOID 1 times on line 1
The line is: QWERPOIAXCWERVOIDWQMAINISDVVOIDQWERYUIZXCHJKERUIP
Found VOID 2 times on line 2
The string: "VOID" was found a total of 3 times in the puzzle
Do you want to search another word (q to quit)?Q