Desenvolva um programa, na linguagem de programação de sua preferência, que apresente os possíveis prefixos, subpalavras e sufixos 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.exercicio04;
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.exercicio04;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/**
* Classe responsavel pela geracao das subpalavras da palavra
*/
public class Subfix
{
/**
* Lista de subpalavras
*/
private Set<String> subfixes;
/**
* Palavra
*/
private String word;
/**
* Construtor para inicializar a palavra
*
* @param word palavra
*/
public Subfix(String word)
{
this.word = word;
subfixes = new TreeSet<String>(new Comparator<String>()
{
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(String sub1, String sub2)
{
if(sub1.length() < sub2.length())
{
return -1;
}
if(sub1.length() > sub2.length())
{
return 1;
}
return sub1.compareTo(sub2);
}
});
subfixes.add("&");
for(int length = 0; length < word.length(); length++)
{
for(int index = 0; index < (word.length() - length); index++)
{
subfixes.add(word.substring(index, length + index + 1));
}
}
}
/**
* Retornar true se a subpalavra fornecida for uma subpalavra da palavra
*
* @param subfix subpalavra fornecido
* @return true se a subpalavra fornecida for uma subpalavra da palavra
*/
public boolean isSubfix(String subfix)
{
return subfixes.contains(subfix);
}
/**
* Retornar a lista de subpalavras
*
* @return lista de subpalavras
*/
public String[] list()
{
return subfixes.toArray(new String[0]);
}
/**
* Retornar a quantidade de subpalavras
*
* @return quantidade de subpalavras
*/
public int size()
{
return subfixes.size();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
StringBuilder out = new StringBuilder();
out.append("{");
for(String subfix : subfixes)
{
if(subfix.length() != word.length())
{
out.append(subfix).append(", ");
}
else
{
out.append(subfix);
}
}
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.exercicio04;
import java.util.LinkedList;
import java.util.List;
/**
* Classe responsavel pela geracao dos sufixos da palavra
*/
public class Suffix
{
/**
* Lista de sufixos
*/
private List<String> suffixes;
/**
* Palavra
*/
private String word;
/**
* Construtor para inicializar a palavra
*
* @param word palavra
*/
public Suffix(String word)
{
this.word = word;
suffixes = new LinkedList<String>();
suffixes.add("&");
for(int index = word.length() - 1; index >= 0; index--)
{
suffixes.add(word.substring(index, word.length()));
}
}
/**
* Retornar true se o sufixo fornecido for um sufixo da palavra
*
* @param suffix sufixo fornecido
* @return true se o sufixo fornecido for um sufixo da palavra
*/
public boolean isSuffix(String suffix)
{
return suffixes.contains(suffix);
}
/**
* Retornar a lista de sufixos
*
* @return lista de sufixos
*/
public String[] list()
{
return suffixes.toArray(new String[0]);
}
/**
* Retornar a quantidade de sufixos
*
* @return quantidade de sufixos
*/
public int size()
{
return suffixes.size();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
StringBuilder out = new StringBuilder();
out.append("{");
for(String suffix : suffixes)
{
if(suffix.length() != word.length())
{
out.append(suffix).append(", ");
}
else
{
out.append(suffix);
}
}
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.exercicio04;
import java.util.Scanner;
/**
* Classe responsavel pela execucao da classe Suffix
*/
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);
Subfix subfix = new Subfix(word);
Suffix suffix = new Suffix(word);
System.out.println("Observação: \"&\" representa a palavra vazia");
System.out.print("Prefixos: ");
System.out.println(prefix);
System.out.print("Subpalavras: ");
System.out.println(subfix);
System.out.print("Sufixos: ");
System.out.println(suffix);
scanner.close();
}
}