|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
public class Stringray extends MiniJava { public static void main(String[] args) { String input = ""; boolean running = true; int mainMenu = -1; while (input.equals("")) input = readString(); while (running) { writeLineConsole("Deine Eingabe ist: "); writeLineConsole(input); writeLineConsole("Du kannst folgende Operationen ausw盲hlen:"); writeLineConsole("0 zum Beenden des Programmes"); writeLineConsole("1 zum Bestimmen der H盲ufigkeit der Buchstaben"); writeLineConsole("2 zum Ersetzen von Buchstaben in der Eingabe"); writeLineConsole("3 zum Wortweisen Spiegeln"); mainMenu = -1; while (mainMenu < 0 || mainMenu > 3) mainMenu = readInt("Was wollen Sie tun?"); String result = ""; int n = input.length(); int i; switch (mainMenu) { case 0: running = false; case 1: int[] frequency = new int[26]; i = 0; while (i < n) { char c = input.charAt(i); if (c >= 'A' && c <= 'Z') { frequency[c - 'A'] += 1; } else if (c >= 'a' && c <= 'z') { frequency[c - 'a'] += 1; } i++; } i = 0; while (i < 26) { if (frequency[i] != 0) result += ((char) (i + 'A') + ": " + frequency[i] + " "); i++; } break; case 2: String toReplace = ""; while (toReplace.length() != 1) toReplace = readString("Bitte geben Sie den zu ersetzenden Buichstaben ein."); String replaceWith = ""; while (replaceWith.length() != 1) replaceWith = readString("Bitte geben Sie den neuen Buchstaben ein."); if (toReplace.charAt(0) >= 96) toReplace = "" + (char) (toReplace.charAt(0) - 32); if (replaceWith.charAt(0) >= 96) replaceWith = "" + (char) (replaceWith.charAt(0) - 32); int diff = toReplace.charAt(0) - replaceWith.charAt(0); i = 0; while (i < n) { if (input.charAt(i) == toReplace.charAt(0) || input.charAt(i) == (toReplace.charAt(0) + 32)) result += (char) (input.charAt(i) - diff); else result += input.charAt(i); i++; } break; case 3: int start = 0; i = 0; while (i < n) { if (input.charAt(i) == ' ' || i == n - 1) { if (i == n - 1) result += input.charAt(i); int j = i - 1; while (j >= start) { result += input.charAt(j); j--; } if (i != n - 1) result += input.charAt(i); start = i + 1; } i++; } break; default: break; } writeLineConsole("Ausgabe: "); writeLineConsole(result); } } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
public class Bienenmajuskel extends MiniJava { public static void main(String[] args) { boolean stopInput = false, firstWord = true; String upper = "", pascal = "", startCase = "", snake = ""; while (!stopInput) { String next = readString("Eingabe beenden mit leerem String"); stopInput = (next.length() == 0); if (stopInput) { write("Uppercase: " + upper + "\n" + "Startcase: " + startCase + "\n" + "PascalCase: " + pascal + "\n" + "Snake-Case: " + snake); } else { // Backup zwecks ung馃惂ltiger Eingabe String bUpper = upper; String bPascal = pascal; String bStartCase = startCase; String bSnake = snake; boolean inputOK = true; int pos = 0; while (pos < next.length()) { char current = next.charAt(pos), small = '?', capital = '?'; if (current >= 'a' && current <= 'z') { small = current; capital = (char) (current + ('A' - 'a')); } else if (current >= 'A' && current <= 'Z') { small = (char) (current + ('a' - 'A')); capital = current; } else { inputOK = false; } upper = upper + capital; if (pos == 0) { pascal = pascal + capital; if (!firstWord) { snake = snake + "_"; } } else { pascal = pascal + small; } if ((pos == 0) && firstWord) { startCase = startCase + capital; } else { startCase = startCase + small; } snake = snake + small; pos++; } if (!inputOK) { write("Ignoriere ung眉ltige Eingabe!"); upper = bUpper; pascal = bPascal; snake = bSnake; startCase = bStartCase; } else { firstWord = false; } } } } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
public class XorEncryption extends MiniJava { public static void main(String[] args) { int key = read("Bitte geben Sie den Schl眉ssel als Zahl zwischen 0 und 63 ein."); while (key < 0 || key > 63) key = read("Ohne Zahl zwischen 0 und 63 geht es nicht weiter..."); int cbc_initial = read("Bitte geben Sie den Initialisierungsvektor zwischen 0 und 63 ein."); while (cbc_initial < 0 || cbc_initial > 63) cbc_initial = read("Ohne Zahl zwischen 0 und 63 geht es nicht weiter..."); String s = readString("Bitte geben Sie einen Text zum Verschl眉sseln ein."); int n = s.length(); // Im ersten Schritt kodieren wir den Text und speichern das Ergebnis in // einem Array ab. int[] textArray = new int[n]; int i = 0; while (i < n) { if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') textArray[i] = s.charAt(i) - 'a'; else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') textArray[i] = s.charAt(i) - 'A' + 26; else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') textArray[i] = s.charAt(i) - '0' + 2 * 26; else if (s.charAt(i) == ' ') textArray[i] = 62; else if (s.charAt(i) == '.') textArray[i] = 63; else { write("Ung眉ltiges Zeichen!"); return; } i++; } // Nun k枚nnen wir die eigentliche Verschl眉sselung durchf眉hren. i = 0; while (i < n) { int cbc; if (i == 0) cbc = cbc_initial; else cbc = textArray[i - 1]; textArray[i] = textArray[i] ^ cbc ^ key; i++; } // Wir dekodieren den Schl眉sseltext und geben das Ergebnis aus. String result = ""; i = 0; while (i < n) { if (textArray[i] < 26) result += "" + (char) ('a' + textArray[i]); else if (textArray[i] < 2 * 26) result += "" + (char) ('A' + textArray[i] - 26); else if (textArray[i] < 2 * 26 + 10) result += "" + (char) ('0' + textArray[i] - 2 * 26); else if (textArray[i] == 62) result += ' '; else if (textArray[i] == 63) result += '.'; i++; } write(result); // Wir f眉hren die Entschl眉sselung durch. i = n - 1; while (i >= 0) { int cbc; if (i == 0) cbc = cbc_initial; else cbc = textArray[i - 1]; textArray[i] = textArray[i] ^ cbc ^ key; i--; } // Wir dekodieren das Ergebnis und geben den dekodierten Text aus. Dieser // sollte identisch mit der urspr眉nglichen Eingabe sein. result = ""; i = 0; while (i < n) { if (textArray[i] < 26) result += "" + (char) ('a' + textArray[i]); else if (textArray[i] < 2 * 26) result += "" + (char) ('A' + textArray[i] - 26); else if (textArray[i] < 2 * 26 + 10) result += "" + (char) ('0' + textArray[i] - 2 * 26); else if (textArray[i] == 62) result += ' '; else if (textArray[i] == 63) result += '.'; i++; } write(result); } } |