1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
-
- public class ROT13 {
-
- Integer diff;
- ROT13(Character cs, Character cf) {
- this.diff=Math.abs((int)cs-(int)cf);
-
- }
-
- ROT13() {
- this.diff = 13;
- }
-
-
- public String crypt(String text) throws UnsupportedOperationException {
- String str="";
-
- for (int i=0; i<text.length(); i++)
- {
- if(!Character.isLetter(text.charAt(i)))
- {
- str+=text.charAt(i);
- }
- else if (Character.isUpperCase(text.charAt(i)))
- {
- char ch = (char)(((int)text.charAt(i) + diff - 65) % 26 + 65);
- str+=ch;
- }
- else if(Character.isLowerCase(text.charAt(i)))
- {
- char ch = (char)(((int)text.charAt(i) + diff - 97) % 26 + 97);
- str+=ch;
- }
- }
- return str;
- }
-
- public String encrypt(String text) {
-
- return crypt(text);
- }
-
- public String decrypt(String text) {
- return crypt(text);
- }
-
- public static String rotate(String string, Character c)
- {
- Integer diff=(int)c-(int)'A';
- return string.substring(diff,string.length())+string.substring(0,diff);
- }
- public static String encrpttextfile() throws FileNotFoundException {
- File file=new File("sonnet18.txt");
- Scanner sc=new Scanner(file);
- StringBuilder br =new StringBuilder();
- while(sc.hasNext())
- {
- ROT13 rot13=new ROT13();
- String str=rot13.crypt(sc.nextLine());
- br.append(str+'\n');
- }
- System.out.println(br);
- return br.toString().trim();
- }
-
- public static void main(String[] args) throws FileNotFoundException {
- encrpttextfile();
- }
-
- }
|