// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class SimpleTransposingCipher {
    public static String transpose(String text) {
        int dim = (int)Math.ceil(Math.sqrt(text.length()));
        char[][] c = new char[dim][dim];
        for(int i = 0; i < dim; i++) {
            for(int j = 0; j < dim; j++) {
                int ix = i * dim + j;
                if(ix < text.length()) {
                    c[i][j] = text.charAt(ix);
                } else {
                    c[i][j] = ' ';
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < dim; i++) {
            for(int j = 0; j < dim; j++) {
                sb.append(c[j][i]);
            }
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        String s = "Dette er en lille test !!!";
        System.out.println("|" + transpose(s) + "|");
        System.out.println("|" + transpose(transpose(s)) + "|");
    }
}
