# Thread: check characterin 2D array

1. ## check characterin 2D array

Hi
how can I check for certain character in rows or columns of 2-D array ?
for example , if array is :
Code:
```int main()
{
int i,j;

char board[3][3]={{'X','X','O'},
{' ','X','O'},
{' ','O','O'}};
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<board[i][j]<<"\t";
cout<<endl;
}
}```
how can I check that if X is used in whole row, return 1.

Thanks

2. Code:
`if(board[1] == "XXX") return 1;`

3. C++ Simple Tic Tac Toe - C And C++ | Dream.In.Code

Don't post again until you've read and understood this
How To Ask Questions The Smart Way

4. Originally Posted by eros
Code:
`if(board[1] == "XXX") return 1;`
don't think that will work since "XXX" is a string literal.

how can I check for certain character in rows or columns of 2-D array ?
you have to iterate over each row of the 2d array to check.

Code:
```int win;
for ( int i = 0 ; i < 3 ; ++i)
{
for( int j = 0 ; j < 3; ++ j)
{
if( board[i] == 'somechar')
win++;
}
if ( win = 3) return 1;
}```

5. here is one way to test for diagonals
Code:
```if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
return 1;```
but i think there is a better way.

Popular pages Recent additions