The today over lesson is about the 5 Methods For Check Palindromes In Java. And I hope this lesson is useful for you.

In the java language programing is, have three different loops:

  • 1. For Loop: First we define a recognize a variable in to for
  • 2. While Loop: first we define recognize a variable on while
  • 3. Do While Loop: The First one, we write the Do keyword, afther we define variable and loop in the java language.

1.While Loop

public class TestWhile {
        // Run | Debug
	public static void main(String[] args) {
		String s = "racear";		
		int left = 0;
		int right = s.length() - 1;
		while(left < right) {
			if(s.charAt(left++) != s.charAt(right--)) {
				System.out.println("Not a Palindrome");
				return;
			}
		}
		System.out.println("Palindrome!");
	}
}
// Not a Palindrome

——————————————————————————————————————————————

2. For Loop

public class ForLoop {
	public static void main(String[] args) {
		String s = "abba";
		for(int i = 0; i < s.length() / 2; i++) {
			if(s.charAt(i) != s.charAt(s.length() -1 -i)) {
				System.out.println("Not a Palindrome!");
				return;
			}
		}
		System.out.println("Palindrome");
	}
}
// Palindrome

——————————————————————————————————————————————

3. Recursion

public class Recursion {
	private static boolean recursivePalindrome(String text, int forward, int backward) {
		if(forward == backward)
			return true;
		
		if((text.charAt(forward)) != (text.charAt(backward)))
			return false;
		
		if(forward < backward + 1)
			return recursivePalindrome(text, forward + 1, backward - 1);
		return true;
	}
	// Run | Debug
	public static void main(String[] args) {
		String s = "abcba";
		if(recursivePalindrome(s, 0, s.length() - 1))
			System.out.println("Palindrome!");
		else
			System.out.println("Not a Palindrome");
	}
}
// Palindrome!

——————————————————————————————————————————————

4. Revers String

public class Reverse_String {
	// Run | Debug
	public static void main(String[] args) {
		String s = "abcdcba";
		if(s.equals(new StringBuilder(s).reverse().toString()))
			System.out.println("Palindrome");
		else
			
			System.out.println("Not a Palindrome");
	}
}
// Palindrome

5. Streams

import java.util.stream.IntStream;
public class Streams {
	// Run | Debug
	public static void main(String[] args) {
		String s = "abcddcba";
		if(IntStream.range(0, s.length() / 2).noneMatch(i -> s.charAt(i) != s.charAt(s.length() -i -1)))
			System.out.println("Palindrome!");
		else
			System.out.println("Not a Palindrome");
	}
}

——————————————————————————————————————————————

Source: Instgram: computer.science.student

I hope this lesson is was useful for you.