Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Added: Dec 12, 2020 9:48 PM
Views: 3
Tags: string
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         String s = "Hallo";
  5.         System.out.println(palindromTest(s));
  6.     }
  7.  
  8.     public static boolean palindromTest (String s){
  9.         //Macht daraus alle leerzeichen und Punctuation weg + toLowerCase()!
  10.         String clean = s.replaceAll("\\W+","").toLowerCase();
  11.         System.out.println(clean);
  12.         int length = clean.length();
  13.         int forward = 0;
  14.         int backward = length - 1;
  15.  
  16.         while(backward > forward){
  17.             char forwardChar = clean.charAt(forward++);
  18.             char backwardChar = clean.charAt(backward--);
  19.  
  20.             if(forwardChar != backwardChar){
  21.                 return false;
  22.             }
  23.         }
  24.         return true;
  25.         }
  26.  
  27.     }
  28.