Desenvolva um programa, na linguagem de programação de sua preferência, que apresente os possíveis prefixos da palavra fornecida pelo usuário.
/*************************************************************************
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.lfa.tutorial01.topico07.exercicio01;
import java.util.LinkedList;
import java.util.List;
/**
* Classe responsavel pela geracao dos prefixos da palavra
*/
public class Prefix
{
/**
* Lista de prefixos
*/
private List<String> prefixes;
/**
* Palavra
*/
private String word;
/**
* Construtor para inicializar a palavra
*
* @param word palavra
*/
public Prefix(String word)
{
this.word = word;
prefixes = new LinkedList<String>();
prefixes.add("&");
for(int index = 1; index <= word.length(); index++)
{
prefixes.add(word.substring(0, index));
}
}
/**
* Retornar true se o prefixo fornecido for um prefixo da palavra
*
* @param prefix prefixo fornecido
* @return true se o prefixo fornecido for um prefixo da palavra
*/
public boolean isPrefix(String prefix)
{
return prefixes.contains(prefix);
}
/**
* Retornar a lista de prefixos
*
* @return lista de prefixos
*/
public String[] list()
{
return prefixes.toArray(new String[0]);
}
/**
* Retornar a quantidade de prefixos
*
* @return quantidade de prefixos
*/
public int size()
{
return prefixes.size();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
StringBuilder out = new StringBuilder();
out.append("{");
for(String prefix : prefixes)
{
if(prefix.length() != word.length())
{
out.append(prefix).append(", ");
}
else
{
out.append(prefix);
}
}
out.append("}");
return out.toString();
}
}
/*************************************************************************
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.lfa.tutorial01.topico07.exercicio01;
import java.util.Scanner;
/**
* Classe responsavel pela execucao da classe Prefix
*/
public class Application
{
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Forneça a palavra desejada: ");
String word = scanner.nextLine().trim();
Prefix prefix = new Prefix(word);
System.out.println("Observação: \"&\" representa a palavra vazia");
System.out.print("Prefixos: ");
System.out.println(prefix);
scanner.close();
}
}