This code defines a class called ROT13 that implements a...

April 23, 2024 at 06:13 AM

package Cypher; import java.util.*; public class ROT13 { String message; public void encode() { Scanner sc = new Scanner(System.in); System.out.println(); System.out.println("ROT13 Cypher"); System.out.print("Message to Encode: "); String message = sc.nextLine(); char[] encodedChars = encodeMessage(message.toCharArray()); System.out.print("Encoded Message: "); for (char character : encodedChars) { System.out.print(character); } System.out.println(); } public void decode() { Scanner sc = new Scanner(System.in); System.out.println(); System.out.println("ROT13 Cypher"); System.out.print("Message to Decode: "); String message = sc.nextLine(); char[] decodedChars = decodeMessage(message.toCharArray()); System.out.print("Decoded Message: "); for (char character : decodedChars) { System.out.print(character); } System.out.println(); } private char[] encodeMessage(char[] chars) { char[] encodedChars = new char[chars.length]; for (int i = 0; i < chars.length; i++) { char ch = chars[i]; if (ch >= 'a' && ch <= 'z') { encodedChars[i] = (char) ((ch - 'a' + 13) % 26 + 'a'); } else if (ch >= 'A' && ch <= 'Z') { encodedChars[i] = (char) ((ch - 'A' + 13) % 26 + 'A'); } else if (ch == 'z') { encodedChars[i] = ')'; } else { encodedChars[i] = ch; } } return encodedChars; } private char[] decodeMessage(char[] chars) { char[] decodedChars = new char[chars.length]; for (int i = 0; i < chars.length; i++) { char ch = chars[i]; if (ch >= 'a' && ch <= 'z') { decodedChars[i] = (char) ((ch - 'a' - 13 + 26) % 26 + 'a'); } else if (ch >= 'A' && ch <= 'Z') { decodedChars[i] = (char) ((ch - 'A' - 13 + 26) % 26 + 'A'); } else if (ch == ')') { decodedChars[i] = 'z'; } else { decodedChars[i] = ch; } } return decodedChars; } }

This code defines a class called ROT13 that implements a simple encryption and decryption technique known as the ROT13 cipher. The class has methods encode() and decode() to encode and decode messages using ROT13. The encodeMessage() method takes a message as input, shifts each letter by 13 positions in the alphabet, and returns the encoded message. The decodeMessage() method reverses this process to decode the message. The code prompts the user to enter a message, encodes or decodes it using ROT13, and then displays the result.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node