-3
    public boolean isPalindrome(String s) {
    for (int i = 0; i < s.length() / 2; i++) {
        if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
            return false;
        }
    }
    return true;
}

This is the answer I got from someone else and I do not know why it uses i<s.length()/2? Also what does s.chatAt(s.length() - i - 1) mean?

Oliver W.
  • 13,169
  • 3
  • 37
  • 50
  • What part don't you understand? Did you read the documentation? – SLaks Feb 10 '15 at 18:20
  • 1
    Cannot understand your question. Please rephrase – Vivin Feb 10 '15 at 18:22
  • 2
    You're gonna ask this question [every day](http://stackoverflow.com/questions/28419014/how-can-i-define-this-code-so-that-it-can-return-true-when-it-comes-to-palindrom) now? What do you not understand of the linked question? Be specific. – runDOSrun Feb 10 '15 at 18:22
  • I hope this is not the answer of an assignment you have. If it is, then try to write your own code. Borrowing code from others won't teach you anything. *edit* Ah, I see, it's the answer of another question. – Tom Feb 10 '15 at 18:23
  • Why do no you step trough the code in debugger for palindrom such as the name 'anna' (lowercased) to have a look? – Michal Feb 10 '15 at 18:24
  • 2
    @Tom yeah, code delivery was yesterday. This is why answering these questions doesn't *help*. OP is back because "gimme the code" wasn't enough. Turns out you need to have some basic understanding a well. – runDOSrun Feb 10 '15 at 18:26
  • Do you have to use the code given? There is an easier way to do it (easier to read especially) – Ascalonian Feb 10 '15 at 18:31

2 Answers2

2

I'll try breaking down the logic for you.

A palindrome is where a word can be read from either direction and still be the same word. For example 'mom'. One way to check if a word is a palindrome is by looking at the word from the first letter, and the last letter at the same time, then going through and checking to see if they are the same letter.

That is what this code is doing. It is making sure that the first and last letter are the same, then moves on to the 2nd and 2nd last letter. If it finds a difference, then it returns an error/false.

To break down a bit of your code: for (int i = 0; i < s.length() / 2; i++) { is looping through your word. Since you are only going to go half way through your word, we only need to go to s.length()/2.

if (s.charAt(i) != s.charAt(s.length() - i - 1)) { checks the character at the start and end, where s.length() -i -1 specifically gets the index for the last character - i (the number of times you've gone through it) -1 (coz index counting is strange).

aashnisshah
  • 468
  • 4
  • 10
0

It divides the length by 2 because it covers 2 characters each iteration. The first iteration, when i is 0, it compares the first and last characters, corresponding to indexes 0 and length - 0 - 1, or length - 1. The second iteration, when i is 1, it compares the second and second to last characters, corresponding to indexes 1 and length - 1 - 1, or length - 2.

If the length is odd, then it won't compare the middle character to anything (index length / 2), but that doesn't matter for palindrome considerations.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • if like the String is "level", so what 's the result of string's length?Is it 5? so that s.charAt(s.length() - 1) would be the character 'l'? – sunqifeng2018 Feb 10 '15 at 20:38
  • Yes, of course the string's length is `5`. So, `s.charAt(0)` and `s.charAt(4)` are compared (the 2 `'l'`s), and `s.charAt(1)` and `s.charAt(3)` are compared (the 2 `'e'`s). – rgettman Feb 10 '15 at 20:41