Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Aufgabe 4) Rekursion mit Strings
- */
- public class Aufgabe4 {
- private static String insertCharValue(String text) {
- String a = "";
- int decimalASCII = text.charAt(0);
- if (text.length() == 1) {
- a = a + '(';
- a = a + decimalASCII;
- a = a + text.charAt(0);
- a = a + ')';
- return a;
- } else {
- a = a + '(';
- a = a + decimalASCII;
- a = a + text.charAt(0);
- a = a + ')';
- return (a + insertCharValue(text.substring(1)));
- }
- }
- private static String removeCharsInString(String text) {
- String pos = text.substring(1);
- int pos2 = pos.indexOf(text.charAt(0));
- // int position = text.substring(1).indexOf(text.charAt(0)); // first char in String
- if (pos2 > 0) { //if char is valid aka char > 0
- return removeCharsInString(text.substring(0, pos2 + 1) + text.substring(pos2 + 2));
- }
- return text.substring(1);
- }
- private static String shiftMaxCharRight(String text) {
- if (text.length() == 0) {
- return "";
- }
- if (text.length() == 1) {
- return text;
- } else {
- if (text.charAt(0) > shiftMaxCharRight(text.substring(1)).charAt(text.length() - 2)) {
- return (text.substring(1) + text.charAt(0));
- }
- else {
- return (text.charAt(0) + shiftMaxCharRight(text.substring(1)));
- }
- }
- }
- public static void main(String[] args) {
- System.out.println(insertCharValue("hallo"));
- System.out.println(insertCharValue("a!"));
- System.out.println(insertCharValue("Ein Test"));
- System.out.println();
- System.out.println(removeCharsInString("testtrompete"));
- System.out.println(removeCharsInString("test"));
- System.out.println(removeCharsInString("t"));
- System.out.println(removeCharsInString("angabe"));
- System.out.println();
- System.out.println(shiftMaxCharRight("acmbwxdzfjdk"));
- System.out.println(shiftMaxCharRight(""));
- System.out.println(shiftMaxCharRight("za"));
- System.out.println(shiftMaxCharRight("a"));
- System.out.println(shiftMaxCharRight("habcdefg"));
- assert (insertCharValue("hallo").equals("(104h)(97a)(108l)(108l)(111o)"));
- assert (insertCharValue("a!").equals("(97a)(33!)"));
- assert (insertCharValue("Ein Test").equals("(69E)(105i)(110n)(32 )(84T)(101e)(115s)(116t)"));
- assert (removeCharsInString("testtrompete").equals("esrompee"));
- assert (removeCharsInString("test").equals("es"));
- assert (removeCharsInString("t").equals(""));
- assert (removeCharsInString("angabe").equals("ngbe"));
- assert (shiftMaxCharRight("acmbwxdzfjdk").equals("acmbwxdfjdkz"));
- assert (shiftMaxCharRight("").equals(""));
- assert (shiftMaxCharRight("za").equals("az"));
- assert (shiftMaxCharRight("a").equals("a"));
- assert (shiftMaxCharRight("habcdefg").equals("abcdefgh"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement