Google Code Jam 2010 - Africa Classification Round
Publicado por Enzo de Brito Ferber (última atualização em 19/12/2011)
[ Hits: 3.784 ]
Homepage: http://www.maximasonorizacao.com.br
Problema B.
http://code.google.com/codejam/contest/dashboard?c=351101#s=p1
Dado um arquivo de entrada com n linhas, ler cada linha do arquivo e imprimir as PALAVRAS em ordem inversa.
Por exemplo:
VIVA O LINUX É MUITO BOM
Imprime:
Case #1: BOM MUITO É LINUX O VIVA
// reverse.c
// Enzo Ferber - dez 2011
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINES 101
#define LETTERS 1001
// reverse word from A to B
void rword ( char *str, int a, int b )
{
char *aux = (char *) malloc ( (b - a + 1) * sizeof ( char ));
register int i;
for ( i = a; i < b; i++ ) *(aux++) = str[i];
for ( i = a; i < b; i++ ) str[i] = *(--aux);
// free memory
free ( aux );
}
// reverse the whole string
void rstr ( char *str )
{
char *backup = (char *) malloc ( strlen(str) * sizeof(char));
register int i, j;
for ( i = 0, j = strlen(str) - 1; i < strlen(str); i++, j-- )
backup[i] = str[j];
strncpy ( str, backup, strlen(str) );
free ( backup );
}
// main function ...
int main ( int argc, char *argv[] )
{
register int i = 0, j = 0, a, lines;
char line[ LINES ][ LETTERS ], c;
FILE *input;
// arguments
if ( argc < 2 )
{
printf ( "Usage: %s <input_file>\n", argv[0] );
exit ( -1 );
}
// try to open input file
if( !(input = fopen(argv[1], "r")))
{
printf ( "# Could not open file %s\n", argv[1] );
exit ( -1 );
}
// read file
while ( (c = fgetc(input)) && !feof(input) )
{
if ( c != '\n' )
line[i][j++] = c;
else
{
line[i][j] = 0x0;
i++;
j = 0;
}
}
// store lines number
lines = i;
// reverse whole string, then words
for ( i = 1; i < lines; i++)
{
rstr ( line[i] );
for ( j = 0; j < strlen(line[i]); j++ )
{
for ( a = j; a <= strlen(line[i]); a++ )
{
if ( line[i][a] == ' ' || line[i][a] == '{FONTE}' )
{
rword(line[i], j, a);
j = a;
break;
}
}
}
}
for ( i = 1; i < lines; i++ )
printf ( "Case #%d: %s\n", i, line[i] );
return 0;
}
Função "Partição de Inteiros" Recursiva COM Tabela Estática em C
Jantar dos Filósofos - Programação Paralela
Jogo Simon (Genius) - com gráficos
Nenhum comentário foi encontrado.
Faça suas próprias atualizações de pacotes/programas no Void Linux e torne-se um Contribuidor
Como rodar o Folding@home no Linux
Criando um painel de controle (Dashboard) para seu servidor com o Homepage
O Abismo entre o Código e o Chão: Saltos Tecnológicos e a Exclusão Estrutural no Brasil
Instalar e Configurar a santíssima trindade (PAP) no Void Linux
Pisando no acelerador do Linux Mint: Kernel XanMod, zRAM e Ajustes de Swap
Como compilar kernel no Linux Mint
Lançamento do Brutal DOOM test 6
Consertando o erro no Brave de webgl
Solução para ter de volta as bordas e barra de títulos das janelas em zenity no Debian 13.x
Seno, Coseno, Tangente em CLIPPER (0)
Inserir uma URL num arquvo pelo Ubuntu (CLIPPER) (0)
VMWare Player não conecta na rede nem consigo intercambiar arquivos (1)









