Create a class called StringCode with the following functions in JAVA

String blowup(String str)
Returns a version of the original string as follows: each digit 0-9 that appears in the original string is replaced by that many occurrences of the character to the right of the digit. So the string "a3tx2z" yields "attttxzzz", and "12x" yields "2xxx". A digit not followed by a character (i.e. at the end of the string) is replaced by nothing.

Respuesta :

tonb

Answer:

public class StringCode {

   public static String blowup(String str) {

       String ret = "";

       for(int i=0; i<str.length();i++)

       {

           char c = str.charAt(i);

           if (Character.isDigit(c)) {

               if (i<str.length()-1) {

                   char d = str.charAt(i+1);

                   ret += String.format("%" + c + "s", d).replace(' ', d);

               }

           }

           else {

               ret += c;

           }

       }

       return ret;

   }

   

   public static void Test(String s) {

       System.out.println(s + " -> " + blowup(s));

   }

   

   public static void main(String []args){

       Test("a3tx2z");

       Test("12x");

       Test("5begin");

       Test("end5");

   }

}

Explanation:

outputs:

a3tx2z -> attttxzzz

12x -> 2xxx

5begin -> bbbbbbegin

end5 -> end