TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java ROT13 Method

Implement the ROT13 cipher. ROT13 is a simple method that rotates characters in the alphabet to obscure text.
ROT13. There are 26 letters in the alphabet. With ROT13, a cipher, we rotate the first 13 with the last 13. This obscures, but does not encrypt, text data.
A String is immutable: it cannot be changed. To modify a String with the ROT13 algorithm in Java we must first convert it to a character array. Then we convert it back to a String.
A method. We introduce an example method. In rot13() we see the character-testing logic. Characters before the middle letter M are rotated backwards, and other are rotated forwards.

String constructor: We pass our character array to the String constructor to convert it back into a String.

Array

Main: Here we rotate a String, and then rotate the rotated string to see if it correctly round-trips (it does).

Java program that uses ROT13 cipher public class Program { public static String rot13(String value) { char[] values = value.toCharArray(); for (int i = 0; i < values.length; i++) { char letter = values[i]; if (letter >= 'a' && letter <= 'z') { // Rotate lowercase letters. if (letter > 'm') { letter -= 13; } else { letter += 13; } } else if (letter >= 'A' && letter <= 'Z') { // Rotate uppercase letters. if (letter > 'M') { letter -= 13; } else { letter += 13; } } values[i] = letter; } // Convert array to a new String. return new String(values); } public static void main(String[] args) { // Rotate the input string. // ... Then rotate the rotated string. String input = "Do you have any cat pictures?"; String rot13 = rot13(input); String roundTrip = rot13(rot13); System.out.println(input); System.out.println(rot13); System.out.println(roundTrip); } } Output Do you have any cat pictures? Qb lbh unir nal png cvpgherf? Do you have any cat pictures?
For punctuation and spaces, no changes are made. Most text remains in a recognizable form, so it is easy to detect ROT13. The algorithm is usually just a learning exercise.
Occasionally, if you want to make text not easily read, using ROT13 is helpful. A sophisticated computer user can decode ROT13, but many users are not sophisticated or don't care.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf