There are given a word, a number n, and a matrix of n by n characters. I have to found the spots where the word is located in the matrix and output the coordinates of the starting point. The word can be found reading from right to left, from left to right, from up to down or from down going up, so 4 directions.
eg:
mere
5
erema
hereb
bmere
bamer
aemre ---> output: 0 3 (right->left)
0 3 (up->down)
2 1 (left->right)
3 2 (down ->up)
Here's the code I wrote in main() but it doesn't show the right thing.
int n, c, x, y, z, x1, t, x2, r, x3; char s[101];
fgets(s, 101, stdin);
printf("n="); scanf_s("%d", &n);
char a[90][90];
for (int i = 0; i < n+1; i++)
{
for (int j = 0; j < n; j++)
{
c = getchar();
a[i][j] = c;
}
}
for (int i = 0; i < n+1; i++)
{
for (int j = 0; j < n; j++)
{
if (a[i][j] == s[0])
{
x = 0; x1 = 0; x2 = 0; x3 = 0; y = j - 1; z = j + 1; t = i + 1, r = i - 1;
for (int k = 1; k < strlen(s); k++) {
if (s[k] == a[i][y])
{
x++; y--;
}
}
if (x == strlen(s) - 1) printf("%d %d\n", i, j);
for (int k = 1; k < strlen(s); k++)
{
if (s[k] == a[i][z])
{
x1++; z++;
}
}
if (x1 == strlen(s) - 1) printf("%d %d\n", i, j);
for (int k = 1; k < strlen(s); k++)
{
if (s[k] == a[t][j])
{
x2++; t++;
}
}
if (x2 == strlen(s) - 1) printf("%d %d\n", i, j);
for (int k = 1; k < strlen(s); k++)
{
if (s[k] == a[r][j])
{
x3++; r--;
}
}
if (x3 == strlen(s) - 1) printf("%d %d\n", i, j);
}
}
}