-
Arrays
Hello,
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
-
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;
intLengthback = intLengthback -1;
for( i =0; i< intMid; i++)
{
if( m_strInput.charAt(i) == m_strInput.charAt(intLengthback))
intLengthback = intLengthback -1;
else
return false;
}
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();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks