
/*************************************************************************
 *  Compilation:  javac StringManips.java
 *  Execution:    java StringManips
 *
 *  Problem 1: Given a string, we will print a new string
 *  made of 3 copies of the last 2 characters
 *  of the original string.
 *  precondition length >= 2
 *  "Hello" -> "lololo"
 *  "hi" -> "hihihi"
 *
 *  Problem 2: Given a string, the program will print
 *  a version without both the first and last characters
 *
 *  "hello" -> "ell"
 *  "abc" -> "b"
 *
 *************************************************************************/

public class StringManips {
    public static void main(String[] args) {
        // Problem 1
        // the original String
        String s = "CIS1100";

        // find the length of s
        int len = s.length();
        // extract the last 2 chars
        String lastChars = s.substring(len - 2, len);
        //String lastChars = s.substring(len - 2);
        String newString = lastChars + lastChars + lastChars;

        System.out.println("New String: " + newString);

        // Problem 2
        // the input String
        String input = "javajava";
        // the length
        len = input.length();
        // delete the first and the last character at the same time
        String withoutFirstLast = input.substring(1, len - 1);

        System.out.println("Without the first and last chars: " + withoutFirstLast);
    }
}
