-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMain.java
More file actions
58 lines (49 loc) · 2.14 KB
/
Main.java
File metadata and controls
58 lines (49 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.groperto;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
showHeader();
String userInput = userInput().toLowerCase(Locale.ROOT);
if (userInput.equals("")) {
System.out.println("-------------------------------------");
System.out.println("String inválida!");
System.out.println("-------------------------------------");
return;
}
String formattedInput = formatInput(userInput);
String result = checkIfPalindrome(formattedInput);
System.out.println("-------------------------------------");
System.out.println(result);
System.out.println("-------------------------------------");
}
public static void showHeader() {
System.out.println("");
System.out.println("############################################################################");
System.out.println("############################### PALINDROMOS ################################");
System.out.println("############################################################################");
System.out.println("");
}
public static String userInput() {
System.out.println("Olá, essa é uma aplicação que verifica se uma dada string é um palíndromo.");
System.out.println("Digite o texto que deseja verificar se é um palíndromo.");
System.out.println("Atenção: Não utilizar acentos e caracteres especiais.");
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
return text;
}
public static String formatInput(String userInput) {
String pattern = "[\\W_]";
return userInput.replaceAll(pattern, "");
}
public static String checkIfPalindrome(String formattedInput) {
char[] text = formattedInput.toCharArray();
int textLength = text.length;
for (int i = 0; i < textLength; i++) {
if (text[i] != text[textLength - 1 - i]) {
return "Não é palíndromo!";
}
}
return "É um palíndromo!";
}
}