Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing java/10-objects/IsPalindrome by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
(Exercise 3.1.13)
A <i>palindrome</i> is a string that reads the same forwards or backwards,
like <tt>"RADAR"</tt> or <tt>"STOOTS"</tt>.
Define a method <tt>isPalindrome</tt> 
that takes as input a string and returns true if the 
string is a palindrome, and false otherwise. You will need to use
the instance methods <tt>charAt()</tt> and <tt>length()</tt>
from the <a href="http://introcs.cs.princeton.edu/java/11cheatsheet/#String">String API</a>.
Public permissions remove
Engine
Template / Reference solution
 
public static boolean isPalindrome(String s) {
\[
// it's only necessary to do half the length many checks
for (int i=0; i<s.length()/2; i++) {
   // look at ith character from start and end
   if (s.charAt(i) != s.charAt(s.length()-i-1))
      return false;
}
return true; // everything matched
]\
}
Java test suite
See manual
 
test("isPalindrome", "racecar");
test("isPalindrome", "ferrari");
test("isPalindrome", "foolproof");
test("isPalindrome", "cool");
test("isPalindrome", "rester");
test("isPalindrome", "redder");
test("isPalindrome", "pinker");
test("isPalindrome", "o");
test("isPalindrome", "ok");
test("isPalindrome", "kk");
test("isPalindrome", "joUO9G");
test("isPalindrome", "rt2$77$2tr");
test("isPalindrome", "Qay&2&yaQ");
test("isPalindrome", "");
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.