// Miya Natsuhara // 07-17-2019 // CSE142 // TA: Grace Hopper // Contains a few short methods displaying the String traversal pattern and a variety of different // String methods and types of String manipulation. public class StringTraversals { public static void main(String[] args) { doubleLetters("banana"); doubleLetters("hello world"); spongeBobify("mod is my favorite operator"); spellOut("mississippi"); spellOut("RESPECT"); spellOut2("mississippi"); spellOut2("RESPECT"); } // Prints the given String with every character appearing twice // String phrase: the phrase to be "doubled" public static void doubleLetters(String phrase) { // Hello --> HHeelllloo // String traversal String result = ""; for (int i = 0; i < phrase.length(); i++) { result = result + phrase.charAt(i) + phrase.charAt(i); } System.out.println(result); } // Prints the given String with every other letter uppercased or lowercased // String phrase: the phrase to be printed with modified casing public static void spongeBobify(String phrase) { // hello --> HeLlO String result = ""; for (int i = 0; i < phrase.length(); i++) { if (i % 2 == 0) { result = result + phrase.toUpperCase().charAt(i); } else { result = result + phrase.toLowerCase().charAt(i); } } System.out.println(result); } // Prints the given String spelled out (with a dash in between each letter) // String phrase: the phrase to be spelled out public static void spellOut(String phrase) { // hello --> h-e-l-l-o // Version pulling out the first post System.out.print(phrase.charAt(0)); for (int i = 1; i < phrase.length(); i++) { System.out.print("-" + phrase.charAt(i)); } System.out.println(); } // Prints the given String spelled out (with a dash in between each letter) // String phrase: the phrase to be spelled out public static void spellOut2(String phrase) { // hello --> h-e-l-l-o // Version pulling out the last post for (int i = 0; i < phrase.length() - 1; i++) { System.out.print(phrase.charAt(i) + "-"); } System.out.println(phrase.charAt(phrase.length() - 1)); } }