Strings-isPalindrome [codingblocks]

Take as input S, a string. Write a function that returns true if the string is a palindrome and false otherwise. Print the value returned.

Input Format
String

Constraints
String length between 1 to 1000 characters

Output Format
Boolean

Sample Input
abba

 
Sample Output
true

 
Explanation
A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is palindrome as it's reverse is "abba", but “abbc” is not palindrome as it's reverse is "cbba".


Java Code:

 
 
/*
Amit Kumar
14-12-2020
IDE Code: https://ide.geeksforgeeks.org/3jrtNa4jPF
*/


package String;
import java.util.Scanner;

public class String_isPalindrome {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        String str = scanner.next();
        if(isPalindrome(str)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }

    private static boolean isPalindrome(String str) {
        int len = str.length();
        if (len < 2) return true;
        int head = 0;
        int tail = len-1;

        while (head<tail) {
            if (str.charAt(head) != str.charAt(tail))
                return false;
            head++;
            tail--;
        }
        return true;
    }
}

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )