Palindrome in java

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.(Wikipedia)

Given a string , print Yes if it is a palindrome, print No otherwise.

Constraints

  •  will consist at most  lower case English letters.

Sample Input:

madam

Sample Output:

yes

Program:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        String R = new StringBuilder(A).reverse().toString();
        
        if(A.equals(R)) System.out.println("Yes");
        else System.out.println("No");
        
        
        
    }
}

Program 2:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        int count = 0;
        for(int i=0; i<A.length()/2;i++)
            if( A.charAt(i) == A.charAt(A.length()-i-1)) count++;
        System.out.println( (count == A.length()/2) ? "Yes" : "No") ;
        
    }
}

Please do subscribe our channel

No comments:

Post a Comment