Java rot13 Hash rot13(String text)

Here you can find the source of rot13(String text)

Description

(De)codes the given text with rot13.

License

Open Source License

Parameter

Parameter Description
text will be (de)coded in rot13

Return

rot13 of text

Declaration

public static String rot13(String text) 

Method Source Code

//package com.java2s;
/*// www . j  av  a 2 s  .c om
GNU General Public License
CacheWolf is a software for PocketPC, Win and Linux that
enables paperless caching.
It supports the sites geocaching.com and opencaching.de
    
Copyright (C) 2006  CacheWolf development team
See http://www.cachewolf.de/ for more information.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

public class Main {
    /**
     * (De)codes the given text with rot13.
     * Text in [] won't be (de)coded.
     *
     * @param text will be (de)coded in rot13
     * @return rot13 of text
     */
    public static String rot13(String text) {
        char[] dummy = new char[text.length()];
        boolean convert = true;
        char c;
        for (int i = 0; i < text.length(); i++) {
            c = text.charAt(i);

            if (convert && ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))) {
                dummy[i] = (char) (c + 13);
            } else if (convert && ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))) {
                dummy[i] = (char) (c - 13);
            } else if (c == '[') {
                convert = false;
                dummy[i] = '[';
            } else if (c == ']') {
                convert = true;
                dummy[i] = ']';
            } else if (c == '<') {
                dummy[i] = '<';
                // reflecting html break (newline) only lowercase
                if (i + 3 < text.length() && text.charAt(i + 1) == 'b' && text.charAt(i + 2) == 'r'
                        && text.charAt(i + 3) == '>') {
                    i++;
                    dummy[i] = 'b';
                    i++;
                    dummy[i] = 'r';
                    i++;
                    dummy[i] = '>';
                } else {
                    if (i + 2 < text.length() && text.charAt(i + 1) == 'p' && text.charAt(i + 2) == '>') {
                        i++;
                        dummy[i] = 'p';
                        i++;
                        dummy[i] = '>';
                    }
                }
            } else {
                dummy[i] = c;
            }
        } // for
        return new String(dummy);
    }
}

Related

  1. rot13(String argInput)
  2. rot13(String input)
  3. rot13(String input)
  4. rot13(String s)
  5. rot13(String string)
  6. rot13(String value)