Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java language Write a class called StringCode with the following functions. (Do

ID: 3890668 • Letter: J

Question

Java language

Write a class called StringCode with the following functions. (Do not change these function names or return types, else the tests will fail). public static 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.(Keep the digit as is)

Explanation / Answer

Please rever t back if you need any further help

Code

public class StringCode {

public static void main(String[] args) {

//Sample strings to verify

String s="a3tx2z";

System.out.println(blowup(s));

s="12x";

System.out.println(blowup(s));

}

//method to encode the string

public static String blowup(String str)

{

// if string is null or empty or string length is 1 we just return string as it is

if(str == null || str.length()==0 || str.length()==1)

return str;

// empty string to store encoded string

String t ="";

// temporary integer used to access the string

int i=0;

//while loop to traverse the string till last but one character

while(i< str.length()-1)

{

// if condition to check if character is a digit

if(Character.isDigit(str.charAt(i)))

{

// if character is a digit appending the next character to temporary string number of times the value of digit

for(int j=0;j< Character.getNumericValue(str.charAt(i));j++)

t+=str.charAt(i+1);

}

//if character is not a digit just append the character to string

else

t+=str.charAt(i);

//increment the index

i++;

}

// for the last character in the string just append it to temporary string as it is

// even if it is a digit we just have to append as there are no other characters right to it

t+=str.charAt(i);

//return the string

return t;

}

}

Sample output

attttxzzz
2xxx

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote