In C, i have a 2D set of characters(word search), I ask the user using get_strin
ID: 644408 • Letter: I
Question
In C, i have a 2D set of characters(word search), I ask the user using get_string to input a word, I then try to find the first charater of the word in the 2D array then I have to check all different ways to find the entire word without getting out of the board. this is the code I have so far but it doesn't work, can you please check what i did wrong and make suggestions, thank yoou.
char c= get_string();
int i, j;
char board[nrows][ncols];
for(i=0; i<nrows; i++){
for(j=0; j<ncols; j++){
if(board[i][j]==c[0])
}}
Explanation / Answer
//C program that prompt user to enter a string and search the first
//character in the board of character array and print the character
//if the first character of string is matched.
#include<stdio.h>
#include<conio.h>
#include<string>
int main()
{
int i, j;
char str[50];
const int nrows=2;
const int ncols=2;
printf("Enter a string : ");
//use gets to read character array
gets(str);
//set a board of size 2 rows and 2 columns
char board[nrows][ncols]={{'f','w'},
{'c','s'}
};
//Note : if board is of character string
//Then declare character array like
//char *board[NUMBER_OF _WORDS][SIZE_OF_WORD]={"initialize words"}
//In search use, strcmp to search for the word in the nested for
//loops
for(i=0; i<nrows; i++)
{
for(j=0; j<ncols; j++)
{
//search for the character in
//the board
if(board[i][j]==str[0])
//if found then print the character
printf("%c ",board[i][j]);
}
}
getch();
return 0;
}
Hope this helps you