-7

I have a string "I love stack overflow very much" how to remove spaces between character and make groups of 8 character? how to add dummy data if no of character does not divided by 8 to make group of 8?how to add dummy data to make groups of 8 characrers?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
hamzasgd
  • 15
  • 5
  • How far have you tried? To generate a random char, you can reference http://stackoverflow.com/questions/5202866/in-java-how-do-you-randomly-select-a-letter-a-z – Antony Dao Nov 24 '16 at 04:12
  • Before your question gets closed, please show us what actual output you want, and also any code you have tried. – Tim Biegeleisen Nov 24 '16 at 04:19

2 Answers2

-1

Here is a one-liner solution using a regex which you can try:

String input = "I love stack overflow very much";
String[] parts = input.replaceAll("\\s", "").split("(?<=\\G.{8})");
String dummy = "X";  // or whatever placeholder you want
parts[parts.length-1] += new String(new char[8 - parts[parts.length-1].length()]).replace("\0", dummy);
System.out.println(Arrays.toString(parts));

This solution pads the final term in the array on the right with Xs, should the final term have less than 8 characters.

Output:

[Ilovesta, ckoverfl, owverymu, chXXXXXX]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
-2
public class Run {
    public static void main(String[] args) {
        String string = "I love stack overflow very much";

        //replacing all newline and and then making tokens
        String[] words = string.replaceAll("\\s", "").split("(?<=\\G.{8})");

        for (String st : words) {
            if (st.length() == 8) { // if length of the string is 8, just print the string
                System.out.println(st);
            } else {
                System.out.print(st);

                // printing dummy characters after the final string
                for (int i = 0; i < 8 - st.length(); i++) {
                    System.out.print("X"); // assuming dummy character = X;
                }
            }
        }

        System.out.println("\n");

        // Reverse
        for (String st : words) {
            if (st.length() == 8) {
                System.out.println(new StringBuilder(st).reverse().toString());
            } else {
                String revString = st;
                for (int i = 0; i < 8 - st.length(); i++) {
                    revString += "X";
                }
                System.out.println(new StringBuilder(revString).reverse().toString());
            }
        }
    }
}

OUTPUT

Ilovesta
ckoverfl
owverymu
chXXXXXX

atsevolI
lfrevokc
umyrevwo
XXXXXXhc
rimonmostafiz
  • 1,341
  • 1
  • 15
  • 33
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. – Makyen Nov 24 '16 at 04:44
  • @Makyen thanks for the tips. I have edited my answer. – rimonmostafiz Nov 24 '16 at 04:57
  • 2
    You shouldn't just give the answer like this. The poster of the question appears to be trying to figure out his homework. Can you re-edit to not include the details of the answer? Thanks! – Alvin Bunk Nov 24 '16 at 05:12
  • hey Rimon Mostafiz thank you – hamzasgd Nov 24 '16 at 06:37
  • In the end how to reverse print every group? Rimon Mostafiz – hamzasgd Nov 24 '16 at 07:01
  • see this http://stackoverflow.com/questions/7569335/reverse-a-string-in-java – rimonmostafiz Nov 24 '16 at 07:31
  • I need this output otsevolI flrevokc umyrevwo xxxxxxhc – hamzasgd Nov 24 '16 at 08:09