Java: Restore the original string by entering the compressed string with this rule
Restore String from Compression Rule
When characters are consecutive in a string, it is possible to shorten it by replacing them with a certain rule. For example, the character string YYYYY, if it is expressed as # 5 Y, it is compressed by one character. 
Write a Java program to restore the original string by entering the compressed string with this rule.
 However, the # character does not appear in the restored character string.
 
Note: The original sentences are uppercase letters, lowercase letters, numbers, symbols, less than 100 letters, and consecutive letters are not more than 9 letters.
Input:
Multiple character strings are given. One string is given per line. 
Output: The restored character string for each character on one line.
Visual Presentation:
Sample Solution:
Java Code:
// Importing the Scanner class from java.util package
import java.util.*;
// Main class named "Main"
public class Main {
    public static void main(String[] args) {
        // Creating a Scanner object to read input from the console
        Scanner stdIn = new Scanner(System.in);
        // Prompting the user to input the text
        System.out.println("Input the text:");
        // Reading the input string
        String str = stdIn.next();
        // Iterating through each character in the input string
        for (int i = 0; i < str.length(); ++i) {
            // Checking if the current character is '#'
            if (str.charAt(i) == '#') {
                // Repeating the next character by the specified number of times
                for (int j = 0; j < (str.charAt(i + 1) - '0'); ++j) {
                    // Printing the repeated character
                    System.out.print(str.charAt(i + 2));
                }
                // Skipping the processed characters (the count and the repeated character)
                i += 2;
            } else {
                // Printing the current character as it is
                System.out.print(str.charAt(i));
            }
        }
        // Printing a new line after processing the input string
        System.out.println();
    }
} 
Sample Output:
Input the text: XY#6Z1#4023 XYZZZZZZ1000023
Flowchart:
 
For more Practice: Solve these Related Problems:
- Write a Java program to restore a string from a compressed format where letters are followed by multi-digit counts.
- Write a Java program to decompress a string that uses nested compression rules.
- Write a Java program to restore a string from a compressed format that uses different symbols for various compression rules.
- Write a Java program to validate and decompress a string only if it strictly follows a specified compression rule.
Go to:
PREV : Count Islands from Grid. 
NEXT : Extract Words of Length 3 to 6 from Sentence.
Java Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
