// Loops Example import java.util.Scanner; public class Loops { public static void main(String[] args) { // Get user input Scanner keyboard = new Scanner(System.in); System.out.print("Enter string: "); String s0 = keyboard.nextLine(); keyboard.close(); // Call example methods String r0 = removeVowels(s0); System.out.println("removeVowels: " + r0); String t0 = duplicateString(s0); System.out.println("duplicateString: " + t0); } // Remove uppercase and lowercase vowels public static String removeVowels(String in) { String out = ""; int index = 0; while (index < in.length()) { char c = in.charAt(index); switch (c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': break; default : out += c; } index++; } return out; } // Duplicate characters in a string public static String duplicateString(String in) { String out = ""; for (int index = 0; index < in.length(); index++) { out += in.charAt(index); out += in.charAt(index); } return out; } }
© 2015 CS160 Colorado State University. All Rights Reserved.