I am student learning Java in high school and I was wondering if you could
help me out.
Can you please tell me how to determine whether a word is a palindrome, (for
example HANNAH or RADAR) using Arrays?
Feel free to reply using source codes.
I would very much appreciated your time and your help,
This is how my program looks right now:
class Palindrome{
public static void main(String str[]){
String word=str[0];
int length = str[0].length()-1;
char charfwd[] = new char[length];
char charbkd[] = new char[length];
int checkback = 0;
for (int i=0; i <= length; i++){
word.charAt(i) = charfwd[i];}
for (int g=length; g >=0; g--)
word.charAt[g] = charbkd[g];
breakOut:
for (int check=0; check <= length; check++) {
checkback = length;
if (charfwd[check].equals(charbkd[checkback])){
checkback = checkback-1;}
else{
System.out.println(word + " is not a palindrome");
break breakOut; }
System.out.println(word + " is a palindrome");
}
}
}
Thank you,
M-Kand
11-01-2000, 07:31 PM
ak
Re: Arrays
Hi I did not test your program. but this is how I wrote.
public class PTest
{
private String m_strInput = null;
public PTest()
{
m_strInput = "RADAR";
if(isPalindrome() == true)
System.out.println("The given word is a palindrome");
else
System.out.println("The given word is NOT a palindrome");
}
public boolean isPalindrome()
{
int intResult = m_strInput.length() %2 ; // modulus operation
int intLengthback = m_strInput.length();
int intMid = intLengthback /2;
int i;
if (intResult != 0) //If the lenght is Even then one more comparision
{
if(m_strInput.charAt(i) == m_strInput.charAt(intLengthback))
return true;
else
return false;
}
else
return true;
}
public static void main(String str[])
{
PTest ptest = new PTest();
}