27S-Section-Solution-1

27S-Section-Solution-1 - CS106A Handout 27S May 4th 6th,...

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

View Full Document Right Arrow Icon
CS106A Handout 27S Spring 2011 May 4 th – 6 th , 2011 Section Solution Discussion Problem 1 Solution: Adding commas to numeric String s private String addCommasToNumericString(String digits) { String result = ""; int len = digits.length(); int nDigits = 0; for ( int i = len - 1; i >= 0; i--) { result = digits.charAt(i) + result; nDigits++; if (nDigits % 3 == 0 && i > 0) { result = "," + result; } } return result; } Discussion Problem 2 Solution: Double Speak private String doubleSpeak(String original) { String transformed = ""; String vowelCluster = ""; for ( int i = 0; i < original.length(); i++) { char ch = original.charAt(i); boolean isVowel = isVowel(ch); if (isVowel) { vowelCluster += ch; } else if (vowelCluster.length() > 0) { transformed += "bb" + vowelCluster; vowelCluster = ""; } transformed += ch; } if (vowelCluster.length() > 0) { transformed += "bb" + vowelCluster; vowelCluster = ""; } return transformed; } private boolean isVowel(char ch) { switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': return true; default : return false ; } }
Background image of page 1

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

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

Page1 / 2

27S-Section-Solution-1 - CS106A Handout 27S May 4th 6th,...

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