8000 Added a Palindrome Checker · RockLee444/Java-Algorithms@73a504c · GitHub
[go: up one dir, main page]

Skip to content

Commit 73a504c

Browse files
authored
Added a Palindrome Checker
Added a little program to know whether or not a sentence is a palindrome.
1 parent be8fff7 commit 73a504c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

String Reversal/Palindrome.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*RockLee444*/
2+
import java.util.Scanner;
3+
public class Palindrome{
4+
public static void main(String[] args){
5+
Scanner sc = new Scanner(System.in);
6+
String word;
7+
System.out.println("Write a sentence and press enter to know if it is a palindrome.");
8+
word = sc.nextLine();
9+
String[] wordArray = word.toUpperCase().split(" ");
10+
String noBlanksWord="";
11+
int numCaracteres=0;
12+
for(int i=0;i<wordArray.length;i++){
13+
numCaracteres+= wordArray[i].length();
14+
noBlanksWord+=wordArray[i];
15+
}
16+
17+
if(verifyWord(noBlanksWord)){
18+
System.out.println("The sentence: " + word + " IS a palindrome.");
19+
} else {
20+
System.out.println("The sentence: " + word + " IS NOT a palindrome.");
21+
}
22+
23+
}
24+
25+
public static boolean verifyWord(String word){
26+
boolean isPalindrome = false;
27+
char[] normalWordArray = word.toCharArray();
28+
char[] inverseWordArray = new char[normalWordArray.length];
29+
30+
int j=0;
31+
//Filling the inverseWordArray
32+
for(int i=normalWordArray.length-1;i>=0;i--){
33+
inverseWordArray[j] = normalWordArray[i];
34+
j++;
35+
}
36+
//Verifying if it is palindrome
37+
int counter=0;
38+
for(int i=0; i<normalWordArray.length;i++){
39+
if(normalWordArray[i] == inverseWordArray[i]){
40+
counter++;
41+
}
42+
}
43+
if(counter==normalWordArray.length){
44+
isPalindrome = true;
45+
}
46+
else{
47+
isPalindrome = false;
48+
}
49+
return isPalindrome;
50+
}
51+
}

0 commit comments

Comments
 (0)
0