Developing Algorithms Using Strings

AP Computer Science A· difficulty 3/5

public static boolean isPal(String s) {
  int i = 0, j = s.length() - 1;
  while (i < j) {
    if (s.charAt(i) != s.charAt(j)) return false;
    i++; j--;
  }
  return true;
}
// call: isPal("racecar")

What does isPal("racecar") return?

  • A

    false

  • B

    null

  • C

    true

    check_circle
  • D

    compile error

Explanation

"racecar" reads the same forwards and backwards.

Want 10 more like this — adaptive to your weak spots?

Related questions