Advertisement
Guest User

natasa.java

a guest
Dec 5th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. /*
  2.     Aufgabe 4) Rekursion mit Strings
  3. */
  4. public class Aufgabe4 {
  5.  
  6.     private static String insertCharValue(String text) {
  7.         String a = "";
  8.         int decimalASCII = text.charAt(0);
  9.         if (text.length() == 1) {
  10.             a = a + '(';
  11.             a = a + decimalASCII;
  12.             a = a + text.charAt(0);
  13.             a = a + ')';
  14.            return a;
  15.         } else {
  16.             a = a + '(';
  17.             a = a + decimalASCII;
  18.             a = a + text.charAt(0);
  19.             a = a + ')';
  20.             return (a + insertCharValue(text.substring(1)));
  21.         }
  22.     }
  23.  
  24.     private static String removeCharsInString(String text) {
  25.         String pos = text.substring(1);
  26.         int pos2 = pos.indexOf(text.charAt(0));
  27.        // int position = text.substring(1).indexOf(text.charAt(0)); // first char in String
  28.         if (pos2 > 0) { //if char is valid aka char > 0
  29.             return removeCharsInString(text.substring(0, pos2 + 1) + text.substring(pos2 + 2));
  30.         }
  31.         return text.substring(1);
  32.     }
  33.  
  34.     private static String shiftMaxCharRight(String text) {
  35.         if (text.length() == 0) {
  36.             return "";
  37.         }
  38.         if (text.length() == 1) {
  39.             return text;
  40.         } else {
  41.             if (text.charAt(0) > shiftMaxCharRight(text.substring(1)).charAt(text.length() - 2)) {
  42.                 return (text.substring(1) + text.charAt(0));
  43.             }
  44.             else {
  45.                 return (text.charAt(0) + shiftMaxCharRight(text.substring(1)));
  46.             }
  47.  
  48.         }
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         System.out.println(insertCharValue("hallo"));
  53.         System.out.println(insertCharValue("a!"));
  54.         System.out.println(insertCharValue("Ein Test"));
  55.         System.out.println();
  56.  
  57.         System.out.println(removeCharsInString("testtrompete"));
  58.         System.out.println(removeCharsInString("test"));
  59.         System.out.println(removeCharsInString("t"));
  60.         System.out.println(removeCharsInString("angabe"));
  61.         System.out.println();
  62.  
  63.         System.out.println(shiftMaxCharRight("acmbwxdzfjdk"));
  64.         System.out.println(shiftMaxCharRight(""));
  65.         System.out.println(shiftMaxCharRight("za"));
  66.         System.out.println(shiftMaxCharRight("a"));
  67.         System.out.println(shiftMaxCharRight("habcdefg"));
  68.  
  69.         assert (insertCharValue("hallo").equals("(104h)(97a)(108l)(108l)(111o)"));
  70.         assert (insertCharValue("a!").equals("(97a)(33!)"));
  71.         assert (insertCharValue("Ein Test").equals("(69E)(105i)(110n)(32 )(84T)(101e)(115s)(116t)"));
  72.  
  73.         assert (removeCharsInString("testtrompete").equals("esrompee"));
  74.         assert (removeCharsInString("test").equals("es"));
  75.         assert (removeCharsInString("t").equals(""));
  76.         assert (removeCharsInString("angabe").equals("ngbe"));
  77.  
  78.         assert (shiftMaxCharRight("acmbwxdzfjdk").equals("acmbwxdfjdkz"));
  79.         assert (shiftMaxCharRight("").equals(""));
  80.         assert (shiftMaxCharRight("za").equals("az"));
  81.         assert (shiftMaxCharRight("a").equals("a"));
  82.         assert (shiftMaxCharRight("habcdefg").equals("abcdefgh"));
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement