• + 0 comments

    Java

    public static String encryption(String s) {
        s = s.replaceAll(" ", ""); 
        int col = (int) Math.ceil(Math.sqrt(s.length()));
        String encrypt = "";
    
        for (int i = 0; i < col; i++) {
            for (int j = 0; j < s.length(); j += col) {
                String str = s.substring(j);
                encrypt += (str.length() - 1 >= i) ? str.charAt(i) : "";
            }
            encrypt += " ";
        }
            
        return encrypt;
    }