Created
March 1, 2018 01:20
-
-
Save wnoizumi/66916da77cbfafe5b5363fed80733063 to your computer and use it in GitHub Desktop.
Escreva um programa em que dado uma frase e a quantidade de colunas que podem ser exibidas na tela, faça a quebra de linhas sem quebrar as palavras.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FormatadorColunas { | |
private int numeroColunas; | |
public FormatadorColunas(int numeroColunas) { | |
this.numeroColunas = numeroColunas; | |
} | |
public String formatarFrase(String frase) { | |
String resultado = ""; | |
String[] palavras = frase.trim().split("\\s+"); | |
String linha = ""; | |
for (String p : palavras) { | |
if (linha.length() + p.length() < numeroColunas) { | |
if (linha.isEmpty()) { | |
linha += p; | |
} else { | |
linha += " " + p; | |
} | |
} else { | |
resultado += linha + "\n"; | |
linha = p; | |
} | |
} | |
if (!linha.isEmpty()) { | |
resultado += linha; | |
} | |
return resultado.trim(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class FormatadorColunasTeste { | |
@Test | |
public void testeFormatador() { | |
FormatadorColunas f = new FormatadorColunas(20); | |
String resultado = f.formatarFrase("Um pequeno jabuti xereta viu dez cegonhas felizes."); | |
String esperado = "Um pequeno jabuti\nxereta viu dez\ncegonhas felizes."; | |
assertEquals(esperado , resultado); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment