# check characterin 2D array

• 02-18-2011
chess2009
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
• 02-19-2011
eros
Code:

`if(board[1] == "XXX") return 1;`
• 02-19-2011
Salem
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
• 02-19-2011
nimitzhunter
Quote:

Originally Posted by eros
Code:

`if(board[1] == "XXX") return 1;`

don't think that will work since "XXX" is a string literal.

Quote:

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; }```
• 02-19-2011
bobknows
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.