-1

I just realized my mistake.

Instead of writing

test1 = do.substring(x, x + 1);
test2 = do.substring(count - 1 - x, count - x);

I wrote

test1 = do.substring(x, x + 1);
test1 = do.substring(count - 1 - x);

I was not aware that you had to "end" a substring, I used this answer. Considering the upvotes it has, I was wondering if someone could explain why it didn't work in my case. How do I get the last character of a string?

om242515
  • 1
  • 2
  • Update: I made it so that the loop would print match/nomatch for each character. I see that the first loop matches, but for the 2nd and 3rd characters there is no match. I used the input "pop". – om242515 Nov 18 '20 at 17:43
  • This is what I got : pop match. nomatch. nomatch. This is not a palindrome. – om242515 Nov 18 '20 at 17:44
  • Not sure if you're allowed to use it, but you could split the input in half and then [use StringBuilder to reverse it](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java) and see if they're equal. Just need to be mindful of odd length Strings when splitting. – Tim Hunter Nov 18 '20 at 17:46
  • I will definitely try to learn more about stringbuilder for personal use, but this is sadly a HS compsci course that's taught for a single exam – om242515 Nov 18 '20 at 17:48
  • You able to use [toCharArray](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#toCharArray())? – Tim Hunter Nov 18 '20 at 17:49
  • Unfortunately I can't, that was another way i was planning to write the program before i was told we could only use substrings – om242515 Nov 18 '20 at 17:52
  • Ugh, despise arbitrary limitations like that. – Tim Hunter Nov 18 '20 at 17:55
  • Just realized my mistake! I didn't define (?) the substring correctly. – om242515 Nov 18 '20 at 17:58
  • The second parameter tells the `substring` method what the ending index should be. Without it, the method assumes you want a substring ending at the far end of the string. – Tim Hunter Nov 18 '20 at 17:58
  • That makes much more sense! Thanks – om242515 Nov 18 '20 at 18:00

3 Answers3

0

try to convert your string to an charArray and then define to indexed one from start of array other from end of array and compare them with each other: if they are always the same then its a Palindrome else its not.

String input = "POP";
char [] values = input.toCharArray();
boolean isPalindrome = true;

for (int i = 0, j = values.length-1; i < values.length /2 && j >= values.length/2; i++, j--) {
    if(values[i] != values[j]) {
        isPalindrome = false;
        break;
    }
}
if(isPalindrome) {
    System.out.println(input + " is a Palindrom");
} else {
    System.out.println(input + " is not a Palindrom");
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
0

I think you don't need to iterate over string when using substrings. You can simply compare left substring with reversed right substring like this:

private static boolean isPalindrome(final String input) {
    int length = input.length();
    int middleIndex = (length % 2 == 0 ) ?
            (length / 2 ) : (length / 2 + 1);
    String left = input.substring(0, length / 2);
    String right = input.substring(middleIndex, length);

    // Compare left string with reverse(right)
    return left.equals(new StringBuilder(right).reverse().toString());
}
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
0

Quoted below is the description of String#substring(int beginIndex, int endIndex):

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Thus, in order to get a string of just one character using String#substring(int beginIndex, int endIndex), the value of endIndex needs to be equal to beginIndex + 1 e.g. "Hello".substring(1, 2) is equivalent to String.valueOf("Hello".charAt(1)).

Demo:

public class Main {
    public static void main(String[] args) {
        // Test strings
        String[] arr = { "dad", "malayalam", "papa" };
        for (String s : arr) {
            System.out.println(isPallindrome(s));
        }
    }

    static boolean isPallindrome(String s) {
        int len = s.length();
        for (int i = 0; i < len / 2; i++) {
            if (!s.substring(i, i + 1).equals(s.substring(len - i - 1, len - i))) {
                return false;
            }
        }
        return true;
    }
}

Output:

true
true
false
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110