// Yazzy Latif // 07/17/2020 // 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("double"); doubleLetters("supercalifragilisticexpialidocious"); spongeBobify("dank memes in computer science"); spongeBobify("mod is my favorite operator"); spellOut("RESPECT"); // find out what it means to me spellOut("mississippi"); } // Prints the given String with every character appearing twice // String phrase: the phrase to be "doubled" public static void doubleLetters(String phrase) { // hello -> hheelloo // 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++) { // result = result + phrase.charAt(i) + phrase.charAt(i); if (i % 2 == 1) { // upper case state result = result + phrase.toUpperCase().charAt(i); } else { // lower case state 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)); } }