HowToDoInJava

  1. Sample Palindrome Program In Java
  2. String Palindrome Program In Cobol Software

. COBOL HELLO WORLD. Example 1 -. by surender, www.suren.space. IDENTIFICATION DIVISION. ENVIRONMENT DIVISION. PROCEDURE DIVISION. DISPLAY 'HELLO WORLD!'. PRG2 Write a program to accept the string from user and to display the same. C Program to Check the Given String is Palindrome Example 1. This program for string palindrome in c allows the user to enter a string (or character array), and a character value. Next, it will check whether the user-specified string is a palindrome string or not.

/ Java Examples / Java program to check palindrome string

Learn to check if a given string is palindrome string with simple java programs using stack, queue or simple loops. In simplest words, a string is palindrome if it is equal to it’s reverse string.

A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, generally if used comma, separators or other word dividers are ignored.

1. Algorithm

1.1. First approach

To check palindrome, we can pick the characters (one by one) from start and end of string and start comparing to each other.

  • Pick first character and last character of string and compare. If both matches – continue. Else string is not palindrome.
  • Pick second character from start and last, compare both. If both matches – continue. Else string is not palindrome.
  • Continue above comparisons till both characters to compare are same or consecutive to each other.

1.2. Second approach

Rather than comparing chars from start and end, we can also find the reverse string of the given string and compare both strings. If both strings are same, they are palindrome.

  • Get character array from given string
  • Build a string by iterating the array from end to beginning index
  • Optionally – remove comma, separators or other word dividers from both strings
  • Compare both strings

In this tutorial, we will see the examples of both approaches.

2. Check palindrome using reverse comparison

This method uses the first approach given above.

3. Check palindrome using StringBuilder.reverse()

StringBuilder.reverse() method is shortest way to reverse a string using library functions.

4. Check palindrome string using java.util.Stack

Using stack’s push() and pop() methods, we can build a reverse string for a given string. Then we compare both strings.

5. Check palindrome string using java.util.Queue

Using Queue’s add() and remove() methods, we can build a reverse string for a given string. Then we compare both strings.

6. Check palindrome string using loops

String

This is simplest approach which simply iterated the char array backwards and creates the string by appending chars to produce reverse string.

Drop me your questions related to check whether the given string is a palindrome or not in java.

Happy Learning !!

Program

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

A palindrome is a string, which when read in both forward and backward ways is the same.

Example:
Example: lol, pop, radar, madam, etc.

Palindrome String Check Program in C++

Explanation:

To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.

Consider a palindrome string: lol,

---------------------------
index: 0 1 2

String

value: l o l
---------------------------

Sample Palindrome Program In Java

To compare it with the reverse of itself, the following logic is used:

String Palindrome Program In Cobol Software

  • 0th character in the char array, string1 is the same as 2nd character in the same string.
    . . . .
  • ith character is the same as 'length-i-1'th character.
  • If anyone of the above condition fails, the flag is set to true(1), which implies that the string is not a palindrome.
  • By default, the value of the flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome.