-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.java
More file actions
55 lines (44 loc) · 1.66 KB
/
Copy pathpalindrome.java
File metadata and controls
55 lines (44 loc) · 1.66 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
package Java;
import java.util.Scanner;
public class palindrome {
public static void main(String args[]) {
// clear unnecessary output in Terminal
System.out.print("\033[H\033[2J");
System.out.flush();
//get user input
String rawInput;
System.out.print("Please enter a String to check if it is a palindrome:\n");
Scanner sc = new Scanner(System.in);
rawInput = sc.nextLine();
sc.close();
// clean the Input
// remove the spaces
String cleanedInput = rawInput.replaceAll("\\s+","");
// remove the special characters
cleanedInput = cleanedInput.replaceAll("[^a-zA-Z0-9]", "");
// convert to uppercase
cleanedInput = cleanedInput.toUpperCase();
// check if entered text is a palindrome by comparing chars
int inputLength = cleanedInput.length();
int firstHalfLength = 0;
int errorCount = 0;
if (inputLength % 2 == 0) {
firstHalfLength = inputLength / 2;
} else {
firstHalfLength = (inputLength - 1) / 2;
}
for (int i = 1; i <= firstHalfLength; i++) {
if (cleanedInput.charAt(inputLength-i) != cleanedInput.charAt(i-1)) {
errorCount += 1;
}
}
// output a result of the check
if (errorCount == 0) {
System.out.println("Yay! Your string is a palindrome!");
} else {
System.out.println("Sorry, this string does not seem to be a palindrome...");
}
// comment in the next line to see the text that is checked
// System.out.println(cleanedInput);
}
}