25-strings

25-strings - import acm.program public class CountUppercase...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Mehran Sahami Handout #25 CS 106A October 24, 2011 Strings Examples Based on examples by Eric Roberts and Patrick Young. Checking for palindromes 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; } Reversing strings and a simpler version of checking for palindromes 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))); } Counting uppercase characters
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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"); } } – 2 – Replace first occurrence public String replaceFirstOccurrence(String str, String orig, String repl) { int index = str.indexOf(orig); if (index != -1) { str = str.substring(0, index) + repl + str.substring(index + orig.length()); } return str; }...
View Full Document

This note was uploaded on 02/09/2012 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 2

25-strings - import acm.program public class CountUppercase...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online