Java Program to Check given string is Palindrome or not

Program will take input as str and Program will check the string is same from both direction. Sample Input 1 adda Sample Output 1 true Sample Inpu
Admin

Program will take input as str and Program will check the string is same from both direction.

Sample Input 1
adda
Sample Output 1
true
Sample Input 2
akdm
Sample Output 2
false
Solution
import java.util.*;
public class Main {
    public static void main(String args[]) {
        // Your Code Here
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        System.out.println(palin(str));

    }
    public static boolean palin (String str) 
    {
        int i=0;
        int j=str.length()-1;
        while(i<j)
        {
            if(str.charAt(i)!=str.charAt(j))
            {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.