A program capable of reading words arranged horizontally, vertically and diagonally in a word-catcher contained in a text file.
Words can be accessed through their coordinates:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef enum {
eHorizontal,
eVertical,
eDiagonal
} orientacao_t;
char obter_letra( int col, int linha, FILE * pf )
{
char buf[1024] = {0};
rewind(pf);
while( 0 <= linha-- )
fgets( buf, sizeof(buf), pf );
return buf[ col ];
}
char * obter_palavra( char * p, int col, int linha, int tam, orientacao_t or, FILE * pf )
{
int i = 0;
for( i = 0; i < tam; i++ )
{
switch( or )
{
case eHorizontal :
p[i] = obter_letra( col + i, linha, pf );
break;
case eVertical:
p[i] = obter_letra( col, linha + i, pf );
break;
case eDiagonal:
p[i] = obter_letra( col + i, linha + i, pf );
break;
default:
break;
}
}
p[tam] = 'CAQIXCBXXJMDVRANWPMT
YGOQZMQWVKMMJMSIAQDI
XFXOBRWILQJDJYORKGWC
UARIHCBJSDTLIEBSETOH
QSLOCAWZFUYXBASBJTCC
NLJDHZMWYMLVPVGWPNAZ
BBTOCWHEYVCDNGPOLHXO
SDICESRPTQPARRLNNSHZ
XMSKYVIGVDZLCEBBIAOW
UTZABEBHKJNTEHFNPILH
GINPIRMZTJJVLIOZHRRE
NJFRQTLRHBQOSNQRIWOX
CBJTPIEDNWBJKGDJRTIC
SRTWSCSPWFHEMKYGYOXU
OYSCYAPEDMABBFJBZSZN
RQVSILWPWOGNREWNKHHE
JBXFOFFYGGATOJLUMORV
FNVLDATMISHLTVIZEDAL
DZADHEZHORIZONTALXTC
MXOPRHRNNNBXMZNHUOOG
';
return p;
}
int main( int argc, char * argv[] )
{
char palavra[50] = {0};
FILE * pf = NULL;
pf = fopen( argv[1], "r" );
printf( "Palavra 1: %s\n", obter_palavra( palavra, 11, 2, 8, eDiagonal, pf ) );
printf( "Palavra 2: %s\n", obter_palavra( palavra, 7, 18, 10, eHorizontal, pf ) );
printf( "Palavra 3: %s\n", obter_palavra( palavra, 5, 8, 8, eVertical, pf ) );
printf( "Palavra 4: %s\n", obter_palavra( palavra, 10, 6, 8, eDiagonal, pf ) );
printf( "Palavra 5: %s\n", obter_palavra( palavra, 9, 16, 4, eHorizontal, pf ) );
printf( "Palavra 6: %s\n", obter_palavra( palavra, 18, 16, 4, eVertical, pf ) );
fclose(pf);
return 0;
}
Test file containing the words VERTICAL
, HORIZONTAL
, DIAGONAL
, GATO
, CACHORRO
and RATO
:
Palavra 1: DIAGONAL
Palavra 2: HORIZONTAL
Palavra 3: VERTICAL
Palavra 4: CACHORRO
Palavra 5: GATO
Palavra 6: RATO
Output:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef enum {
eHorizontal,
eVertical,
eDiagonal
} orientacao_t;
char obter_letra( int col, int linha, FILE * pf )
{
char buf[1024] = {0};
rewind(pf);
while( 0 <= linha-- )
fgets( buf, sizeof(buf), pf );
return buf[ col ];
}
char * obter_palavra( char * p, int col, int linha, int tam, orientacao_t or, FILE * pf )
{
int i = 0;
for( i = 0; i < tam; i++ )
{
switch( or )
{
case eHorizontal :
p[i] = obter_letra( col + i, linha, pf );
break;
case eVertical:
p[i] = obter_letra( col, linha + i, pf );
break;
case eDiagonal:
p[i] = obter_letra( col + i, linha + i, pf );
break;
default:
break;
}
}
p[tam] = 'CAQIXCBXXJMDVRANWPMT
YGOQZMQWVKMMJMSIAQDI
XFXOBRWILQJDJYORKGWC
UARIHCBJSDTLIEBSETOH
QSLOCAWZFUYXBASBJTCC
NLJDHZMWYMLVPVGWPNAZ
BBTOCWHEYVCDNGPOLHXO
SDICESRPTQPARRLNNSHZ
XMSKYVIGVDZLCEBBIAOW
UTZABEBHKJNTEHFNPILH
GINPIRMZTJJVLIOZHRRE
NJFRQTLRHBQOSNQRIWOX
CBJTPIEDNWBJKGDJRTIC
SRTWSCSPWFHEMKYGYOXU
OYSCYAPEDMABBFJBZSZN
RQVSILWPWOGNREWNKHHE
JBXFOFFYGGATOJLUMORV
FNVLDATMISHLTVIZEDAL
DZADHEZHORIZONTALXTC
MXOPRHRNNNBXMZNHUOOG
';
return p;
}
int main( int argc, char * argv[] )
{
char palavra[50] = {0};
FILE * pf = NULL;
pf = fopen( argv[1], "r" );
printf( "Palavra 1: %s\n", obter_palavra( palavra, 11, 2, 8, eDiagonal, pf ) );
printf( "Palavra 2: %s\n", obter_palavra( palavra, 7, 18, 10, eHorizontal, pf ) );
printf( "Palavra 3: %s\n", obter_palavra( palavra, 5, 8, 8, eVertical, pf ) );
printf( "Palavra 4: %s\n", obter_palavra( palavra, 10, 6, 8, eDiagonal, pf ) );
printf( "Palavra 5: %s\n", obter_palavra( palavra, 9, 16, 4, eHorizontal, pf ) );
printf( "Palavra 6: %s\n", obter_palavra( palavra, 18, 16, 4, eVertical, pf ) );
fclose(pf);
return 0;
}