How to Check If String is Palindrome

In this example, we’ll check if String is Palindrome or not.

Palindrome: A sequence of characters that reads the same backward as forward.

Input:
String a = "RADAR";

Output:
true

1. Java

#Approach 1

public class StringPalindrome {

	public static void main(String[] args) {
		StringPalindrome obj = new StringPalindrome();
		System.out.println(obj.isPalindrome("obo"));
	}

	public boolean isPalindrome(String text) {
		
		String reverseString = "";
		for (int i = text.length() - 1; i >= 0; i--) {
			reverseString += text.charAt(i);
		}
		if(text.equals(reverseString)) {
			return true;
		}
		return false;
	}

}

Output:

true

#Approach 2

public boolean isPalindrome(String s) {

		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
				return false;
			}
		}
		return true;
}

Complete Java Solutions can be found here: Github Link

The complete list can be found here: Practise Programming Questions