File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments