public static String encode(String text, int offset) {
        char[] chars = text.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            int c = chars[i];
            if (c >= 'a' && c <= 'z') {
                c += offset;
                if (c > 'z') {
                    c = 'a' + (c - 'z' - 1);
                } else if (c < 'a') {
                    c = 'z' - ('a' - c) + 1;
                }
            } else {
                c += offset;
                if (c > 'Z') {
                    c = 'A' + (c - 'Z' - 1);
                } else if (c < 'A') {
                    c = 'Z' - ('A' - c) + 1;
                }
            }
            chars[i] = (char)c;
        }
        return new String(chars);
    }