Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looping Through Strings. String Loops Usually a for-loop – do you know why? Where would the loop start? Where would the loop stop?

Similar presentations


Presentation on theme: "Looping Through Strings. String Loops Usually a for-loop – do you know why? Where would the loop start? Where would the loop stop?"— Presentation transcript:

1 Looping Through Strings

2 String Loops Usually a for-loop – do you know why? Where would the loop start? Where would the loop stop?

3 What does it do? for(int i = 0; i< word.length(); i++){ char letter = word.charAt(i); System.out.println(letter); }

4 Running Totals Sometimes our “running total” or “accumulator” variable should be a string. Start it at “” Use concatenation to add to it

5 Creating a String String word = “Happy”; String newWord = “”; for(int i = 0; i< word.length(); i++){ char letter = word.charAt(i); newWord = newWord + letter; }

6 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: H e y

7 CodingBat: No Loops Warmup: makeTags left2 right2 hasBad seeColor Intermediate: twoChar endsLy frontAgain without2 Tricky: conCat withoutX

8 CodingBat: Loops Warmup: doubleChar countHi Intermediate: repeatEnd sameStarChar mixString repeatSeparator Tricky: catDog zipZap


Download ppt "Looping Through Strings. String Loops Usually a for-loop – do you know why? Where would the loop start? Where would the loop stop?"

Similar presentations


Ads by Google