

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Based on examples by Eric Roberts and Patrick Young.
public boolean isPalindrome(String str) { for(int i = 0; i < str.length() / 2; i++) { if (str.charAt(i) != str.charAt(str.length() - (i + 1))) { return false; } } return true; }
public String reverseString(String str) { String result = ""; for(int i = 0 ; i < str.length(); i++) { result = str.charAt(i) + result; } return result; }
public boolean simpleIsPalindrome(String str) { return (str.equals(reverseString(str))); }
import acm.program.*;
public class CountUppercase extends ConsoleProgram {
private int countUppercase(String str) { int upperCount = 0; for(int i=0; i<str.length(); i++) { char ch = str.charAt(i); if (Character.isUpperCase(ch)) { upperCount++; } } return upperCount; }
public void run() { String str = readLine("Enter String: "); println(countUppercase(str) + " upper case letters"); } }
public String replaceFirstOccurrence(String str, String orig, String repl) { int index = str.indexOf(orig); if (index != -1) { str = str.substring(0, index)