0

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);
        }
    }
}

                     
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
DvdiidI
  • 63
  • 6
  • Please note that the first time you call `c = getchar();` it will read the newline. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). And if you enter data in the nested loops line by line, then each line ends also in newline which is always read by `getchar()` and ditto if you enter the data with each character individually followed by newline. It's just another character to `getchar()`. – Weather Vane Nov 21 '20 at 18:10
  • Ok, but I still can't see what's the problem with my code – DvdiidI Nov 21 '20 at 19:47
  • Have you dealt with the newline characters yet? – Weather Vane Nov 21 '20 at 19:50
  • How can I actually do that on my code? I tried to get rid of the new line after scanf_s by adding a getchar() after but still doesn't work. – DvdiidI Nov 21 '20 at 20:06
  • By ignoring every input to `getchar()` that is a newline. So where you have `c = getchar();` change that to `while((c = getchar()) == '\n') {}` – Weather Vane Nov 21 '20 at 20:09
  • Ok, done that; now I can input 10 lines so double and shows nothing – DvdiidI Nov 21 '20 at 20:18
  • I think you should employ the debugging tool to analyse exactly what the code is doing. – Weather Vane Nov 21 '20 at 20:21

1 Answers1

0

Another getchar() after s_scanf() just to consume the '\n' character should do it.

printf("n="); scanf_s("%d", &n);
getchar();

This will only work if you want to get rid of the '\n' character left by scanf().

alex01011
  • 1,670
  • 2
  • 5
  • 17