




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!





3 - 1
Reading: Downey: Chapter 7 Problem Set: Assignment #1 due Tuesday, Feburary 13 Wellesley College CS Lecture 03 Monday, February 5 Handout # (Revised Friday, February 9) 3 - 2
Characters are the fundamental unit of strings & files Character literals:
3 - 3
ASCII = American Standard Code for Information Exchange Defines integer representations for 2^8 = 256 characters (see http://www.asciitable.com):
public static boolean isLetter (char c) { return (โaโ <= c && c <= โzโ) || (โAโ <= c && c <= โZโ); } public static boolean isDigit (char c) { return (โ 0 โ <= c && c <= โ 9 โ); } public static boolean isUpperCase (char c) { // isLowerCase is similar return โAโ <= c && c <= โZโ; } public static char toLowerCase (char c) { // toUpperCase is similar return isUpperCase(c)? (char) (((int) c) + 32) : c; } public static String toString (char c) { return โโ + c; } public static char shiftChar (char c, int shift) { return (char) MathOps.mod(((int) c) + shift, 256); } // MathOps.mod(x,m) returns x modulo m
3 - 7
String s1 = โdeliveryโ; s1.length() โ 8 s1.charAt(3) โ โiโ s1.charAt(8) โ StringIndexOutOfBoundsException s1.indexOf(โeโ) โ 1 s1.indexOf(โqโ) โ -1 // Indicates character is not present s1.indexOf(โeโ, 2) โ 5 s1.indexOf(โeโ, 9) โ -1 // No exception here! s1.indexOf(โliveโ) โ 2 s1.indexOf(โloveโ) โ - s1.indexOf(โliveโ, 9) โ -1 // No exception here! 3 - 8
s1.substring(4) โ โveryโ s1.substring(8) โ โโ s1.substring(9) โ StringIndexOutOfBoundsException s1.substring(2, 6) โ โliveโ s1.substring(4,8) โ โveryโ s1.substring(5,5) โ โโ s1.substring(6,9) โ StringIndexOutOfBoundsException s1.substring(7,6) โ StringIndexOutOfBoundsException s1.toUpperCase() โ โDELIVERYโ (s1.toUpperCase()).toLowerCase() โ โdeliveryโ s1.toCharArray() โ {โdโ, โeโ, โlโ, โiโ, โvโ, โeโ, โrโ, โy}
3 - 9
String s2 = "I say \t"Yes"\nyou say \t"No"\nI say \t"Why?""; System.out.println(s2); I say "Yes" you say "No" I say "Why?" s2.length() โ 40 s2.charAt(6) โ โ\tโ s2.charAt(7) โ โ\โโ s2.charAt(12) โ โ\nโ s2.replace('s', 'S') โ "I Say \t"YeS"\nyou Say \t"No"\nI Say \t"Why?"" s2.replaceAll("say", "said") โ "I said \t"Yes"\nyou said \t"No"\nI said \t"Why?"" s2.replaceAll("[\t\n]", " ") // [\t\n] is a regular expression denoting a tab or a newline. โ "I say "Yes\โ you say "No\โ I say "Why?"โ "aabaaacadaaaae".replaceAll(โa+โ,โXYZโ) โ โXYZbXYZcXYZdXYZeโ // a+ is a regular expression denoting any nonempty sequence of aโs "p \t \n q \n rs \t t".replaceAll("\s+", " ") โ โp q rs tโ // \s+ is a regular expression denoting any nonempty sequence of whitespace // characters (e.g., spaces, tabs, newlines, carriage returns). The extra backslash // is needed to pass the backslash before the s to the regular expression checker. 3 - 10
s2.split("\n") โ {"I say \t"Yes"", "you say \t"No"", "I say \t"Why?""} s2.split("[\n\t]"); โ {"I say ", ""Yes"", "you say ", ""No"", "I say ", ""Why?""} s2.split("[\n\t ]"); // [\n\t ] is a regular expression denoting a // newline or a tab or a space. โ {"I", "say", "", ""Yes"", "you", "say", "", ""No"", "I ", "say", "", ""Why?""} // The empty strings come from three space/tab combinations in the input. s2.split("\s+"); // see notes on \s+ on the previous slide โ {"I", "say", ""Yes"", "you", "say", ""No"", "I ", "say", ""Why?""} // Using \s+ removes the empty strings "\t \na\t \nb\t \n".trim() โ "a\t \nb" How do we convert strings like "{ 17 , -3 , 42 }" to the corresponding integer arrays (e.g. {17,-3,42})? (See IntArray.fromString() in utils directory.) See Java API of the String class for the complete list of String methods.
3 - 13
public static String toUpperCase (String s) { } How would we modify the above to implement the following? public static String replace (String s, char oldChar, char newChar); public static String shiftString (String s, int shift); // StringOps.shiftString("HAL", 1) โ "IBM" 3 - 14
public static String toUpperCase (String s) { }
3 - 15
// StringOps.removeChar(โdeliveryโ, โeโ) โ โdlivryโ // StringOps.removeChar(โdeliveryโ, โdโ) โ โeliveryโ // StringOps.removeChar(โdeliveryโ, โqโ) โ โdeliveryโ public static String removeChar (String s, char c) { } 3 - 16
// StringOps.reveree(โdeliveryโ) โ โyreviledโ public static String reverse (String s) { }