What are palindromes? or Why are palindromes important? Your computer can solve this problem with its built in function, but why? In this article we’ll find out.


What are palindromes? or Why are palindromes important? Your computer can solve this problem with its built in function, but why? In this article we’ll find out.

A palindrome is a string of text that reads the same backward and forward, for example “Rise to vote, sir”, or “Was it a car or a cat I saw?”. Mathematically speaking, we say that the string s is a palindrome if the string formed by reversing all of its characters is equal to itself.

String s = “Rise to vote, sir”;

String reversed = new StringBuffer(s).reverse().toString();

System.out.println(s); //prints “Rise to vote, sir”

System.out.println(reversed); //prints “ris etov ot esir”

System.out.println(s.equals(reversed)); //prints false

We can see from the above example that our string “Rise to vote, sir” is not a palindrome because the reversed string does not match itself exactly.

But what makes this problem important? Can’t we just use our brain to tell if something

What is a palindrome?

In a previous article, I showed how to create a palindrome program in java. A palindrome is a word that reads the same backward or forward. For example, some common single word palindromes include kayak and radar. Some phrase-length palindromes include “Madam, I’m Adam” or “Able was I ere I saw Elba.”

The word “palindrome” was first coined by the English playwright Ben Jonson in the 17th century from the Greek roots palin (“again”) and dromos (“way, direction”).

Why are palindromes important for computer programmers?

Palindromes are important for computer programming because they are useful for writing certain types of code. For example, if you want to write a routine that reverses a string, it helps to have test cases with which to verify that your code is working correctly. Palindroms make good test cases because they have an expected output: the string reversed is itself.

Creating palindromes also gives you practice creating looping constructs in your code. If you think about

import java.util.Scanner;

class PalindromeTest

{

public static void main(String args[])

{

String a, b = “”;

Scanner s = new Scanner(System.in);

System.out.print(“Enter the string you want to check:”);

a = s.nextLine();

int n = a.length();

for(int i = n – 1; i >= 0; i–)

{

b = b + a.charAt(i);

} if(a.equalsIgnoreCase(b))

{

System.out.println(“The string is palindrome.”); } else { System.out.println(“The string isn’t palindrome.”); } } }

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

// input the string

Scanner scan = new Scanner(System.in);

System.out.print(“Enter a string: “);

String str = scan.nextLine();

if (isPalindrome(str)) {

System.out.println(“The string is a palindrome.”);

} else {

System.out.println(“The string isn’t a palindrome.”);

}

}

public static boolean isPalindrome(String s) {

if (s.length() == 0 || s.length() == 1) {

return true;

}

if (s.charAt(0) == s.charAt(s.length() – 1)) {

return isPalindrome(s.substring(1, s.length() – 1));

}

return false;

public class Palindrome{

public static void main(String[] args){

String str = “”;

str = args[0];

if (isPalindrome(str)){

System.out.println(“YES”);

}else{

System.out.println(“NO”);

}

}

private static boolean isPalindrome(String str){

if (str == null){ return false; }

int length = str.length(); for (int i = 0; i < length/2; i++){ if (str.charAt(i) != str.charAt(length-i-1)){ return false; } } return true; } } A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers. Examples in English: Was it a car or a cat I saw? No 'x' in Nixon. Murder for a jar of red rum. Doc, note: I dissent. A fast never prevents a fatness. I diet on cod. Some people have erroneously claimed that "Go hang a salami; I'm a lasagna hog" is the longest palindromic sentence in the English language. However, this sentence is not grammatically correct and uses invented terms. The longest correct sentence is: "A man, a plan, a canoe, pasta, heros' rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, Sara Lee pies--li It is often useful to determine whether a string is a palindrome. A palindrome is a word, phrase, number, or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). Some examples of one-word palindromes include the English words pop, racecar, kayak, and radar. Palindrome phrases like "Go hang a salami; I'm a lasagna hog", "Able was I ere I saw Elba" and "A man, a plan, a canal: Panama" are less common. Also included are numerical palindromes such as "1881", "1992" and "2002". The term "palindromic" is used for ideas, concepts or numbers that can be read in either direction. For example, the number 1001 (one thousand and one) can be read the same way backwards and forwards. We could also call it a palindromic number.


Leave a Reply

Your email address will not be published.