Ask Experts Questions for FREE Help !
Ask
    BluBlack's Avatar
    BluBlack Posts: 1, Reputation: 1
    New Member
     
    #1

    Oct 19, 2011, 06:52 AM
    Help with palindromes in Java arrays
    I'm trying to make something to detect palindromes in a 1D array. It works fine for just 1 array, but I am required to shift all the numbers in the array to the right a few times, and determine whether each sequence is a palindrome. When I add the code to shift the arrays, it doesn't properly detect the palindromes.
    An example of a run:

    1 2 3 2 1
    This is a palindrome.
    1 1 2 3 2
    This is not a palindrome.
    2 1 1 2 3
    This is not a palindrome.

    This is the related code:


    Right = seqsize-1;
    firstInt = sequence[seqsize-1];
    //Shifting to the right
    while (right > 0){
    sequence[right] = sequence[right-1];
    right--;
    }
    sequence[0] = firstint;


    left = 0;
    right = seqsize;
    while (left < right){
    if (sequence[left] != sequence[right-1]){
    palindrome = 0;
    left = right 1;
    }
    else{
    palindrome = 1;
    }
    left ;
    right--;
    }


    Left is a counter for the first array value, and right is a counter for the last array value.
    If I remove the first part, where it shifts the array values to the right, it works just fine. But it doesn't work properly when I include it, so I figured the problem lies there. Also, I am just a beginner in Java, and have only learned the basics - if statements, while loops, functions... So if possible I'd like to accomplish this with just what I know.

    Any help would be much appreciated, thanks.

Check out some similar questions!

All 6-digit palindromes [ 2 Answers ]

I am trying to find a two 6-digit palindromes whose difference would be less than 50. Thank you!

Random numbers , variable arrays [ 1 Answers ]

here is my problem To create a program that generates 7 random numbers display it in 7 labels (not repeat same number twice)and there a button if u click it picks out the highest number. Im a beginner so I need code.


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.