package com.harvard.cs60;
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
System.out.println(“Enter a string to check if it is a palindrome:”);
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine();
if (isPalindrome(inputString)) {
System.out.println(“The input String is a palindrome.”);
} else {
System.out.println(“The input String is not a palindrome.”);
}
}
public static boolean isPalindrome(String inputString) {
int length = inputString.length(); // find the length of the input string int i, begin, end, middle; // declare that you are going to use these variables in the following code
begin = 0; // start at the beginning of the string, index 0
end = length – 1; // set end to be just before the end of the string by doing length – 1 (0 based counting)
middle = (begin +
import java.util.*;
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
System.out.println(“Enter a string to check if it is a palindrome: “);
Scanner scanner = new Scanner(System.in);
String inputString = scanner.nextLine();
String reverse = reverseString(inputString);
// Checking whether the string is palindrome or not
if (inputString.equals(reverse)) {
System.out.println(“The input string is a palindrome.”);
} else {
System.out.println(“The input string is not a palindrome.”);
}
}
// Method to reverse any string
public static String reverseString(String input) {
char[] inp = input.toCharArray();
for (int i = 0; i < inp.length / 2; i++) { char temp = inp[i]; inp[i] = inp[inp.length - 1 - i]; in public class Palindrome { public static void main(String[] args) { String str, rev = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check if it is a palindrome"); str = in.nextLine(); int length = str.length(); for ( int i = length - 1; i >= 0; i– )
rev = rev + str.charAt(i);
if (str.equals(rev))
System.out.println(“Entered string is a palindrome.”);
else
System.out.println(“Entered string is not a palindrome.”);
} }
The following program is a program that determines if a word is a palindrome. It is important to be able to identify a palindrome to avoid grammatical errors in writing.
The word “palindrome” was coined from Greek roots palin (πάλιν; “again”) and dromos (δρóμος; “way, direction”), by English writer Ben Jonson in the 17th century. The word “palindrome” first appeared in English in the late 16th century and has been used primarily as an adjective. A common example of a palindrome is the word racecar, which reads the same forward or backward, although backwards it spells racecar.
The following program uses a while loop and an array to determine if a word is a palindrome. It shows how efficient arrays are because they are able to store information without having to use variables for each character of the string.
public class Palindrome {
public static void main(String [] args) {
for (int i = 0; i < args.length; i++) {
String s = args[i];
System.out.println(s + " is palindrome? " + isPalindrome(s));
}
}
public static boolean isPalindrome(String s) {
if (s == null || s.length() <= 1) return true;
if (s.charAt(0) != s.charAt(s.length()-1)) return false;
return isPalindrome(s.substring(1, s.length() - 1));
}
}
//Palindrome.java
import java.util.*;
import java.io.*;
public class Palindrome
{
public static void main(String[] args) throws Exception {
//for time test:
long start = System.currentTimeMillis();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
ArrayList
for (;;) { //loop forever…or until the user enters an empty line.
System.out.print(“Please enter a word or phrase (enter blank line to quit): “);
String line = input.readLine().trim(); //get rid of leading and trailing whitespace on the line
if (line.equals(“”)) break; //if they entered a blank line, exit the loop
String s = getLetters(line).toLowerCase();
if (isPalindrome(s)) {
palindromes.add(“\”” + line + “\” IS a palindrome”);
} else {
palindromes.add(“\”
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print(“Input a word:”);
String str = in.nextLine();
System.out.println(“The word ‘” + str + “‘ is a palindrome: ” + isPalindrome(str));
}
public static boolean isPalindrome(String s) {
if (s == null || s.isEmpty()) {
return true;
}
int i = 0, j = s.length() – 1;
while (i < j) { if (s.charAt(i) != s.charAt(j)) { return false; } i++; j--; } return true; } }