80 likes | 229 Views
Looping Through Strings. String Loops. Usually a for-loop – do you know why? Where would the loop start? Where would the loop stop?. What does it do?. for(int i = 0; i < word.length (); i ++){ char letter = word.charAt( i ) ; System.out.println(letter ) ; }. Running Totals.
E N D
String Loops • Usually a for-loop – do you know why? • Where would the loop start? • Where would the loop stop?
What does it do? for(inti = 0; i< word.length(); i++){ char letter = word.charAt(i); System.out.println(letter); }
Running Totals • Sometimes our “running total” or “accumulator” variable should be a string. • Start it at “” • Use concatenation to add to it
Creating a String String word = “Happy”; String newWord = “”; for(inti = 0; i< word.length(); i++){ char letter = word.charAt(i); newWord = newWord + letter; }
Practice • Write a loop that takes a String and returns the string with all the e’s removed. For example, removeE(“elephant”) should return “lphant” • public String removeE(String word) • Write a loop that prints a word vertically. • public void verticalPrint(String word) • For example, verticalPrint(“Hey”) prints: Hey
CodingBat: No Loops • Warmup: • makeTags • left2 • right2 • hasBad • seeColor • Intermediate: • twoChar • endsLy • frontAgain • without2 • Tricky: • conCat • withoutX
CodingBat: Loops • Warmup: • doubleChar • countHi • Intermediate: • repeatEnd • sameStarChar • mixString • repeatSeparator • Tricky: • catDog • zipZap