Check string for palindrome

前端 未结 30 3177
悲哀的现实
悲哀的现实 2020-11-22 02:47

A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.

To check whether a word is a palindrome I get th

30条回答
  •  别那么骄傲
    2020-11-22 03:20

    Using stack, it can be done like this

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String str=in.nextLine();
            str.replaceAll("\\s+","");
            //System.out.println(str);
            Stack stack=new Stack();
            stack.push(str);
            String str_rev=stack.pop();
            if(str.equals(str_rev)){
                System.out.println("Palindrome"); 
            }else{
                 System.out.println("Not Palindrome");
            }
        }
    }
    

提交回复
热议问题