GATE CSE
First time here? Checkout the FAQ!
x
0 votes
24 views

Input

You are given a template in which you need to implement a function whose signature is given below.

C
int findWordInAGrid(char grid[128][128], int m, int n, char word[32])
/* return 0 for false, 1 for true. */

C++
bool findWordInAGrid(char grid[128][128], int m, int n, char word[32])

Java
static boolean findWordInAGrid(char[][] grid, int m, int n, String word)

grid[][] represents the characters that are in the given grid. Only the first m rows and the first n columns should be considered relevant. word is the word whose occurance has to be found in the grid. Remember, word may start from any location in the grid. word will never contain more than 30 characters. Return true if word is found in the grid, false otherwise.

 

Output

The function should return true, if you can find the word in the grid and false otherwise.

Example

Let's consider the grid given below:

a b c
d e f
g h i

And set of words to be searched are:

abc
abedhi
efgh

Output:

The output of the above example should be:

abc: true
abedhi: true
efgh: false

 

Constraints

1 ≤ m,n ≤ 100

 

 

asked in Puzzles by (21 points)   | 24 views

1 Answer

0 votes
int findWordInAGrid(char grid[128][128], int m, int n, char word[32]){
    char str[30];
    int c=0;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            if(grid[i][j]==word[c]){
                str[c]=grid[i][j];
                c++;
            }
        }
        if(strcmp(str,word)==0){
            return 1;
        }else{
            strcpy(str,"");
            c=0;
        }
    }
    return 0;
}
answered by (493 points)  


Top Users Sep 2017
  1. Habibkhan

    7184 Points

  2. Warrior

    2664 Points

  3. Arjun

    2582 Points

  4. rishu_darkshadow

    2520 Points

  5. A_i_$_h

    2280 Points

  6. nikunj

    1980 Points

  7. manu00x

    1856 Points

  8. makhdoom ghaya

    1770 Points

  9. Bikram

    1744 Points

  10. SiddharthMahapatra

    1718 Points


26,151 questions
33,733 answers
79,970 comments
31,120 users