1 / 8

Looping Through Strings

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.

mateo
Download Presentation

Looping Through Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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(inti = 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(inti = 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: Hey

  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

More Related