package coding4fun;

/**
 * There are a series of alphanumeric sequences delimited by a (or a sequence of) non-alphanumeric
 * sequence. Reverse each the alphanumeric sequences such as below.
 *
 * <pre>
 * Input : /olleh123?dlrow!
 * Output: /321hello?world!
 *
 * Input : 1234567890
 * Output: 0987654321
 *
 * Input : 123/[]\abc()-=
 * Output: 321/[]\cba()-=
 * </pre>
 */
public class Problem52 {
    public static String reverse(String str) {
        // TODO
        return str;
    }

    public static void main(String[] args) {
        System.out.println(reverse("/olleh123?dlrow!"));
        System.out.println(reverse("1234567890"));
        System.out.println(reverse("123/[]\\abc()-="));
    }
}

@@ -361,126 +361,1152 @@
 =%0A *
- %3C/pre%3E%0A */%0Apublic class Problem52 %7B%0A    public static String reverse(String str) %7B%0A        // TODO%0A        return str
+%0A * Input : /!@123/%5B%5D%5Cabc()-=%0A * Output: /!@321/%5B%5D%5Ccba()-=%0A *%0A * Input : /!@&%25%5E-=%0A * Output: /!@&%25%5E-=%0A * %3C/pre%3E%0A */%0Apublic class Problem52 %7B%0A    public static String reverse(String str) %7B%0A        char%5B%5D chars = str.toCharArray();%0A        int prev = 0;%0A        int current = 0;%0A        boolean first = true;%0A        for (; current %3C chars.length; current++) %7B%0A            if (!isAlphanumeric(chars%5Bcurrent%5D)) %7B%0A                reverse(chars, prev, current - 1);%0A                prev = current;%0A                first = true;%0A            %7D else %7B%0A                if (first) %7B%0A                    prev = current;%0A                    first = false;%0A                %7D%0A            %7D%0A        %7D%0A        reverse(chars, prev, current - 1);%0A        return new String(chars);%0A    %7D%0A%0A    private static void reverse(char%5B%5D chars, int i, int j) %7B%0A        for (; i %3C j; i++, j--) %7B%0A            char tmp = chars%5Bj%5D;%0A            chars%5Bj%5D = chars%5Bi%5D;%0A            chars%5Bi%5D = tmp;%0A        %7D%0A    %7D%0A%0A    private static boolean isAlphanumeric(char c) %7B%0A        return ((c %3E= 'a' && c %3C= 'z') %7C%7C%0A            (c %3E= 'A' && c %3C= 'Z') %7C%7C%0A            (c %3E= '0' && c %3C= '9'))
 ;%0A  
@@ -1607,24 +1607,44 @@
 3?dlrow!%22));
+ // /321hello?world!
 %0A        Sys
@@ -1678,24 +1678,38 @@
 34567890%22));
+ // 0987654321
 %0A        Sys
@@ -1752,16 +1752,175 @@
 ()-=%22));
+ // 321/%5B%5D%5Ccba()-=%0A        System.out.println(reverse(%22/!@123/%5B%5D%5C%5Cabc()-=%22)); // /!@321/%5B%5D%5Ccba()-=%0A        System.out.println(reverse(%22/!@&%25%5E-=%22)); // /!@&%25%5E-=
 %0A    %7D%0A%7D

Solve problem 52