How to check whether a sentence is palindrome in Java

1 Answer

0 votes
public class CheckWhetherSentenceIsPalindrome {
    public static boolean isSentencePalindrome(String str) {
        // Change the string into lowercase and remove all non-alphanumeric characters
        str = str.toLowerCase().replaceAll("[^a-zA-Z0-9]", "");
          
        return str.equals(new StringBuilder(str).reverse().toString());
    }
      
    public static void main(String args[]) {
        System.out.println(isSentencePalindrome("Top step's pup's pet spot.") ? "yes": "no"); 
    }
}

  
   
/*
run:
     
yes
    
*/

 



answered Jun 30, 2024 by avibootz
edited Aug 23, 2024 by avibootz

Related questions

1 answer 119 views
1 answer 118 views
1 answer 123 views
1 answer 121 views
1 answer 125 views
1 answer 115 views
1 answer 106 views
...