// Main program

public class Main {

    public static void main(String[] args) {

        String string1 = "Hello ";
        String string2 = "There";
        String string3 = "!@#$%^&*()What's left?";
        String string4 = "HELLO there 0123456789 *&^*&%^";

        StringUtilities utils = new StringUtilities();
        
        String string5 = utils.replicateString(string1, 3);
        System.out.println("replicateString: \""+string5+"\"");

        String string6 = utils.concatenateStrings(string1, string2);
        System.out.println("concatenateStrings: \""+string6+"\"");

        String string7 = utils.removeSpecialCharacters(string3);
        System.out.println("removeSpecialCharacters: \""+string7+"\"");

        String string8 = utils.reverseCase(string1);
        System.out.println("reverseCase: \""+string8+"\"");
        
        String string9 = utils.reverseString(string3);
        System.out.println("reverseString: \""+string9+"\"");
        
    }

}

// String utilities class

public class StringUtilities {

    // Replicates strings
    // Pre-conditions: Inputs a Java string and a number >= 0
    // Post-conditions: Returns a string that replicates the input n timess
    public String replicateString(String string, int number) {
        String s = "";
        for (int i = 0; i < number; i++) {
            s += string;
        }
        return s;
    }

    // Concatenates strings
    // Pre-conditions: Input two Java strings
    // Post-conditions: Returns a string that is concatenates two inputs
    public String concatenateStrings(String string1, String string2) {
        return (string1 + string2);
    }

    // Removes special characters
    // Pre-conditions: Input a Java string
    // Post-conditions: Returns a copy of input string with special chars
    public String removeSpecialCharacters(String string) {
        String s = "";
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (Character.isDigit(c) || Character.isLetter(c) || c == ' ')
                s += c;
        }
        return s;
    }

    // Reverse case
    // Pre-conditions: Input a Java string
    // Post-conditions: Returns a copy of input string with case reversed
    public String reverseCase(String string) {
        String s = "";
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (Character.isUpperCase(c))
                s += Character.toLowerCase(c);
            else
                s += Character.toUpperCase(c);
        }
        return s;
    }

    // Reverse case
    // Pre-conditions: Input is a Java string
    // Post-conditions: Returns a copy of input string with chars reversed
    public String reverseString(String string) {
        String s = "";
        for (int i = string.length()-1; i >= 0; i--) {
            char c = string.charAt(i);
            s += c;
        }
        return s;
    }
}

© 2012 CS160 Colorado State University. All Rights Reserved.