Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// File: Palindrome.java // This program determines whether an input line is a p

ID: 3605037 • Letter: #

Question


// File: Palindrome.java // This program determines whether an input line is a palindrome. The program // ignores everything except alphabetic letters, and it ignores the difference // between upper- and lowercase letters.
import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.Scanner;
public class Palindrome { /** * The main method prompts the user for a strings. Each string is read * and checked to see whether it is a palindrome. The program ends when * the user enters an empty line (just the return key). * @param args * not used in this implementation **/ public static void main(String[ ] args) { Scanner stdin = new Scanner(System.in); // Keyboard input    String line; // One input line    do { System.out.println("Type a sentence or press Enter to quit."); line = stdin.nextLine( );    if (line.length() != 0) { if (is_palindrome(line)) System.out.println("That is a palindrome."); else System.out.println("That is not a palindrome."); } // end if } while (line.length( ) != 0); }
/** * Determine whether a string is a palindrome. Note: All non-letters are * ignored, and the case of the letters is also ignored. * @param input * the string to check * @return * true if and only if the input string is a palindrome. **/ public static boolean is_palindrome(String input) {    Queue<Character> q = new LinkedList<Character>( ); Stack<Character> s = new Stack<Character>( ); Character letter; // One character from the input string int mismatches = 0; // Number of spots that mismatched int i; // Index for the input string    for (i = 0; i < input.length( ); i++) { letter = input.charAt(i); if (Character.isLetter(letter)) { letter = Character.toUpperCase(letter); q.add(letter); s.push(letter); } }    while (!q.isEmpty( )) { if (q.remove( ) != s.pop( )) mismatches++; }
// If there were no mismatches, then the string was a palindrome. return (mismatches == 0); }    } // File: Palindrome.java // This program determines whether an input line is a palindrome. The program // ignores everything except alphabetic letters, and it ignores the difference // between upper- and lowercase letters.
import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.Scanner;
public class Palindrome { /** * The main method prompts the user for a strings. Each string is read * and checked to see whether it is a palindrome. The program ends when * the user enters an empty line (just the return key). * @param args * not used in this implementation **/ public static void main(String[ ] args) { Scanner stdin = new Scanner(System.in); // Keyboard input    String line; // One input line    do { System.out.println("Type a sentence or press Enter to quit."); line = stdin.nextLine( );    if (line.length() != 0) { if (is_palindrome(line)) System.out.println("That is a palindrome."); else System.out.println("That is not a palindrome."); } // end if } while (line.length( ) != 0); }
/** * Determine whether a string is a palindrome. Note: All non-letters are * ignored, and the case of the letters is also ignored. * @param input * the string to check * @return * true if and only if the input string is a palindrome. **/ public static boolean is_palindrome(String input) {    Queue<Character> q = new LinkedList<Character>( ); Stack<Character> s = new Stack<Character>( ); Character letter; // One character from the input string int mismatches = 0; // Number of spots that mismatched int i; // Index for the input string    for (i = 0; i < input.length( ); i++) { letter = input.charAt(i); if (Character.isLetter(letter)) { letter = Character.toUpperCase(letter); q.add(letter); s.push(letter); } }    while (!q.isEmpty( )) { if (q.remove( ) != s.pop( )) mismatches++; }
// If there were no mismatches, then the string was a palindrome. return (mismatches == 0); }    } A: Download Palindrome.java from this assignment page in Canvas, then do the following steps o Change the class name to WordPalindrome and save the file as WordPalindrome.java. o Change the header comments. o Add a method named isWordPalindrome. (You can copy and paste the code from the isPalindrome method into this new method, but don't remove or change the isPalindrome method.) The method will accept a String parameter and will return a boolean value: true if the parameter is a word palindrome (the words read the same forward and backward), and false if not. (Word palindrome is described in Problem 1 on page 406.) The end of a word is marked by either You may assume that the String contains only letters, apostrophes, and spaces. a space or the end of the String iswordPalindrome must use the Java Queue interface and the Java LinkedList class. Programming Hint: A Scanner can be set up to take its input from a String. Shown below is a code segment that will print the words from a String, one word per line. String sentence "This is a sentence." Scanner s new Scanner( sentence ) while (s.hasNext()) ( System.out.printin(s.next() o Modify the main method. Inside the loop, -Input a string from the user. Keep the if statement that checks the length of the string, then inside the if statement: Create a second string that has only the letters, apostrophes, and spaces from the first string (all other characters removed) Call the isWordPalindrome method and print an appropriate message with the result. The loop should terminate when the user just presses Enter. Submit WordPalindrome.java

Explanation / Answer

class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;  
int n=454;//It is the number variable to be checked for palindrome

temp=n;  
while(n>0){  
   r=n%10; //getting remainder
   sum=(sum*10)+r;  
   n=n/10;  
}  
if(temp==sum)  
   System.out.println("palindrome number ");  
else  
   System.out.println("not palindrome");  
}
}