-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathPalindrome.java
More file actions
50 lines (46 loc) · 1.59 KB
/
Palindrome.java
File metadata and controls
50 lines (46 loc) · 1.59 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
/*RockLee444*/
import java.util.Scanner;
public class Palindrome{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String word;
System.out.println("Write a sentence and press enter to know if it is a palindrome.");
word = sc.nextLine();
String[] wordArray = word.toUpperCase().split(" ");
String noBlanksWord="";
for(int i=0;i<wordArray.length;i++){
noBlanksWord+=wordArray[i];
}
if(verifyWord(noBlanksWord)){
System.out.println("The sentence: " + word + " IS a palindrome.");
} else {
System.out.println("The sentence: " + word + " IS NOT a palindrome.");
}
sc.close();
}
public static boolean verifyWord(String word){
boolean isPalindrome = false;
char[] normalWordArray = word.toCharArray();
char[] inverseWordArray = new char[normalWordArray.length];
int j=0;
//Filling the inverseWordArray
for(int i=normalWordArray.length-1;i>=0;i--){
inverseWordArray[j] = normalWordArray[i];
j++;
}
//Verifying if it is palindrome
int counter=0;
for(int i=0; i<normalWordArray.length;i++){
if(normalWordArray[i] == inverseWordArray[i]){
counter++;
}
}
if(counter==normalWordArray.length){
isPalindrome = true;
}
else{
isPalindrome = false;
}
return isPalindrome;
}
}