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.
|