Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the following elements in a a given string?

Tags:

java

string

Given an input:

a3b4c5

Output should be:

aaabbbbccccc

How i did this program?

I have a string from user as given in the input and then checked whether its alphabet first.
If it's a alphabet, increment till it reaches its counter number to which it is to be printed.
I tried to produce the same output, but i am getting a following line as output.

Output shown:

Code:

public class Pattern
{
    public static void main(String s[]) throws NumberFormatException, IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s1 = br.readLine();
        char ch[] = s1.toCharArray();
        for (int i = 0; i < ch.length;) {
            if ((ch[i] <= 65) || (ch[i] >= 90)) {
                i++;
            } else if ((ch[i] <= 97) || (ch[i] >= 122)) {
                i++;
            } else {
                if (ch[i] == '0' || ch[i] == '1' || ch[i] == '2'
                        || ch[i] == '3' || ch[i] == '4' || ch[i] == '5'
                        || ch[i] == '6' || ch[i] == '7' || ch[i] == '8'
                        || ch[i] == '9') {
                    for (int p = 0; p < ch[i]; p++) {
                        System.out.println(ch[i - 1]);

                    }
                }
                i++;
            }
        }
    }
}
like image 401
javaCoderMakeSimple Avatar asked Jan 28 '26 12:01

javaCoderMakeSimple


2 Answers

Example code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Pattern
{
    public static void main(String s[]) throws NumberFormatException, IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter input string: ");
        String inputStr = br.readLine();

        ArrayList<String> stringParts = new ArrayList<>();
        Matcher matcher = Pattern.compile("\\D+|\\d+").matcher(inputStr);
        while (matcher.find())
        {
            stringParts.add(matcher.group());
        }

        // Just to view the contents
        System.out.println("stringParts: " + Arrays.toString(stringParts.toArray()));

        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < stringParts.size(); i += 2) {
            for(int j = 0; j < Integer.parseInt(stringParts.get(i + 1)); j++) {

                sb.append(stringParts.get(i));
            }
        }

        System.out.println("Output: " + sb.toString());
    }
}

Input/Output:

Enter input string: a3b4c5
stringParts: [a, 3, b, 4, c, 5]
Output: aaabbbbccccc

Enter input string: ab2cd3
stringParts: [ab, 2, cd, 3]
Output: ababcdcdcd

Enter input string: abc1def2xyz3
stringParts: [abc, 1, def, 2, xyz, 3]
Output: abcdefdefxyzxyzxyz


  • Using a pattern matcher, split the input string into chunks of alphabets and digits
  • Use a string builder to append them accordingly


Note:

This should be some form of Run-length encoding, or decoding in this case.

like image 76
almightyGOSU Avatar answered Jan 30 '26 02:01

almightyGOSU


This should fix your problem

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char ch[] = br.readLine().toCharArray();

for (int i = 1; i < ch.length; i++) {
    if (Character.isDigit(ch[i])) {
        for (int j = 0; j < ch[i]-'0'; j++) {
            System.out.print(ch[i-1]);
        }
    }
}

Basically by having the single if statement that checks whether ch[i] is a digit or not, you avoid having to do all of your other checks. If ch[i] is a digit, convert it to an integer by subtracting the character '0' from it. The rest should make sense.

like image 36
blasko Avatar answered Jan 30 '26 02:01

blasko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!