I am writing a method to decrypt a input string. The encryption is straight forward. Any repeating character in the string is replaced by the character followed by the number of times it appears in the string. So, hello is encrypted as hel2o. Below is the decryption logic I have so far. It works but is so imperative and involves multiple loops. How can this be improved?
String input = "hel2o"; String[] inarr = input.split("\\s+"); StringBuilder sb = new StringBuilder(); for(int i = 0; i < inarr.length; i++) { String s = inarr[i]; char[] c = s.toCharArray(); for(int j = 0; j < c.length; j++) { if(Character.isDigit(c[j])) { for(int x = 0; x < Character.getNumericValue(c[j])-1; x++) { sb.append(c[j-1]); } } else { sb.append(c[j]); } } } System.out.printl(sb.toString());