Arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Arrays

  1. #1
    Kand Guest

    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





  2. #2
    ak Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center