Answer to Question #22294 in Java | JSP | JSF for MM

Question #22294
A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same letter to be equal.for example,the following are palindromes:
1.warts n straw
2.radar
3.able was I ere I saw Elba
4.xyzczyx
Write a program that will accept a sequence of characters terminated by a period and will dedide whether the string--without the period---is a palindrome.You may assume that the input contains only letters and blanks and is at most 80 characters long.Include a looop that allows the user to check additional strins until she or he requests that the program ind. Hint Define a static method called isPalindrome
that begins as follows:
/**
Precondition: The array a contains letters and blanks in positions a[0] through a[used - 1]. Returns true if the string is a palindrome and false otherwise.
*/
public static boolean isPalindrome(char[] a, int used)

Your program shoud read the input characters int an arra y whose base type is char and then call the preceding method.the int variable used keeps track of how nuch of the array is used, as described in the section entitled "Partially Filled Arrays."
1
Expert's answer
2013-01-17T09:17:12-0500
package Polindrom;

import java.util.Scanner;

public class main {

publicstatic void main(String[] args) {
char[]symb = new char[80];

Scannercs = new Scanner(System.in);

inti = 0;
intch;
while((ch = cs.nextLine().toCharArray()[0]) != 46)// reads from console
{
symb[i]= (char) ch;
i++;
}

System.out.println(isPalindrome(symb,i));
}

publicstatic boolean isPalindrome(char[] a, int used) {
char[]tmp = new char[a.length];

intind = 0;
for(int i = 0; i < used - 1; i++) {
if(a[i] != 32) {
tmp[ind]= a[i];
ind++;
}
}

intn = (ind) / 2;

Strings1 = "";
Strings2 = "";
for(int i = 0; i < n; i++) {

s1+= a[i];
s2+= a[used - 1 - i];
if(!s1.equalsIgnoreCase(s2)) {
returnfalse;
}
}
returntrue;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS