How to check Palindrome String in java?

 

Introduction

In this article, we will find string palindromes in Java, it works the same as in integer. The meaning of the palindrome string in java is that if we start reading it from right to left or left to right it is the same in both cases.

Algorithm

Approach 1

For checking string palindromes in java, we can choose the characters one by one from the start and end of the string and compare each other.

= Choose the first and the last character of the string and compare, if both the characters match then continue, else the string is not a palindrome.

= Choose the second character from the first and last of the string and compare, if both match - continue, else return string is not a palindrome.

= Continue the above comparisons till both the characters to compare are the same or adjacent to each other.

Approach 2 

Instead of comparing the characters from start to end, we can find the reverse string of the given string and compare both the strings, if they are the same that means the string is a palindrome.

= From given string, get character array

= Create a string by iterating the array from the end to the start index.

= Remove separator, comma, or other dividers from both the strings (Optional)

= Finally, compare both the strings

Example

  1. String Palindrome in java using Loop

import java.util.Scanner;

public class Main 

{

  public static void main(String[] args) 

  {

    System.out.print("Enter any string : ");

    Scanner in = new Scanner(System.in);

    String origString = in.nextLine();

    String reverseString = "";


read full article...

Comments

Popular posts from this blog

Mock Test For Various Companies

What is the process of TCS's next step?

How Mock Tests Will Help You To Ace The Entrance Examinations?