the below program should take an 8x8 grid as input then how many words it will find and then words it is going to found. Then if it found a word it should replace it with a hashtag ('#'). Input spesification includes newlines like this ("\n" represented as newline) :
AWESOMEA(newline)
DEKTTADR(newline)
DZLZQUOC(newline)
RPEYRMDL(newline)
EEAEAPRY(newline)
SFFNQBDA(newline)
SSFTMASN(newline)
AWESOGER(newline)
5(newline)
ADDRESS(newline)
STUDY(newline)
LEAF(newline)
ROMAN(newline)
AWESOME(newline)
Code gives an
Exception thrown at 0x7ABCEE21 (ucrtbased.dll) in wordgrid.exe: 0xC0000005: Access violation reading location 0x52444441. At VS2019. Not sure why. Any help is appreciated.
All hep is appreciated Thanks a lot :) Here is the code :
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
int main()
{
bool isword();
int i, j, wordcount, numofwords;
char tempgrid[8][8];
char grid[8][8];
char* words[15];
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
scanf("%c", &grid[i][j]);
}
scanf("\n");
}
scanf("\n");
scanf("%d", &numofwords);
scanf("\n");
for (i = 0; i < numofwords;i++)
{
scanf("%[^\n]%*c", words[i]);
scanf("\n");
}
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
tempgrid[i][j] = grid[i][j];
}
}
for (wordcount = 0, i = 0; (wordcount < numofwords) && (i < 8); i++, wordcount++)
{
for (j = 0; j < 8;j++)
{
if (isword(grid, words[wordcount], i, j))
tempgrid[i][j] = '#';
}
}
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
grid[i][j] = tempgrid[i][j];
}
}
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
printf("%c", grid[i][j]);
}
printf("\n");
}
return 0;
}
bool isword(char grid[8][8], char* word, int heightcurrent, int widthcurrent)
{
int y[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int x[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
int k, map, dirx, diry;
int wordlen;
if (grid[heightcurrent][widthcurrent] != word[0])
{
return false;
}
wordlen = strlen(word);
for (map = 0; map < 8; map++)
{
diry = heightcurrent + y[map];
dirx = widthcurrent + x[map];
for (k = 1; k < wordlen ;k++)
{
if (diry > 8 || diry < 0 || dirx > 8 || dirx < 0)//checking if out of bounds
break;
if (grid[diry][dirx] != word[k])
break;
diry += x[map];
dirx += y[map];
}
if (k + 1 == wordlen)
return true;
}
return false;
}
User contributions licensed under CC BY-SA 3.0