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:
IDE Code: https://ide.geeksforgeeks.org/3jrtNa4jPF
/* 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
Post a Comment