hi everyone, i am completely out of ideas why i'm getting WA, so pls help!
this is my solution (JAVA) to problem 401 palindromes:
the comments are not included when i submit, and the classname is changed to Main also.
[java]
class palindrome{
static char[] input=null;
static boolean isPalindrome=false;
static boolean isMirroredString=false;
static void main(){
int a;
char[] b=new char[19];
int count=0;
boolean EOFreached=false;
while(EOFreached==false){
try{
while((a=System.in.read())!=(int)'\n'){
if(a==-1){ //is this the right code for detecting EOF?
EOFreached=true;
break;
}
b[count]=(char)a;
count++;
}
}catch(Exception e){}
char[] c=new char[count];
for(int i=0; i<count; i++)
c[i]=b[i];
checkPalindrome(c);
checkMirroredString(c);
String s=new String(c);
if(isPalindrome==true){
if(isMirroredString==true)
System.out.println(s+" -- is a mirrored palindrome.");
else
System.out.println(s+" -- is a regular palindrome.");
}
else if(isPalindrome==false){
if(isMirroredString==true)
System.out.println(s+" -- is a mirrored string.");
else
System.out.println(s+" -- is not a palindrome.");
}
isPalindrome=false;
isMirroredString=false;
count=0;
}
}
static void checkPalindrome(char[] p){ //simply creates a reverse of the string see if its the same as b4
char[] r=new char[p.length];
for(int i=0; i<p.length; i++)
r[p.length-(i+1)]=p[i];
if(equals(r,p))
isPalindrome=true;
}
static void checkMirroredString(char[] c){ //chages chars to their reverses and then creates a reverse of the whole thing to compare to input
char[] r=new char[c.length];
for(int i=0;i<c.length;i++){
if(c[i]=='B' || c[i]=='C' ||c[i]=='D' || c[i]=='F' || c[i]=='G' || c[i]=='K' ||c[i]=='N' || c[i]=='P' || c[i]=='Q'
|| c[i]=='R' || c[i]=='4' || c[i]=='6' || c[i]=='7' || c[i]=='9'){
isMirroredString=false;
break;
}
else if(c[i]!='A'&&c[i]!='H'&&c[i]!='I'&&c[i]!='M'&&c[i]!='O'&&c[i]!='T'&&c[i]!='U'&&c[i]!='V'&&c[i]!='W'&&c[i]!='X'
&&c[i]!='Y'&&c[i]!='1'&&c[i]!='8'){
if(c[i]=='E')
r[c.length-i-1]='3';
else if(c[i]=='L')
r[c.length-i-1]='J';
else if(c[i]=='S')
r[c.length-i-1]='2';
else if(c[i]=='Z')
r[c.length-i-1]='5';
else if(c[i]=='2')
r[c.length-i-1]='S';
else if(c[i]=='3')
r[c.length-i-1]='E';
else if(c[i]=='5')
r[c.length-i-1]='Z';
}
else
r[c.length-(i+1)]=c[i];
}
if(equals(r,c))
isMirroredString=true;
}
static boolean equals(char[] a, char[] b){
for(int i=0;i<a.length;i++){
if(a[i]!=b[i]){
return false;
}
else
continue;
}
return true;
}
}
[/java]
thx!
