fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static String duplicatePrefix(final String source, final int prefixLength, final int count) {
  12. if (source == null || source.length() == 0 || prefixLength <= 0 || count <= 0) {
  13. return "";
  14. }
  15.  
  16. final int actual = Math.min(source.length(), prefixLength);
  17. final int size = actual * count;
  18. final char[] results = new char[size];
  19. // copy the first prefix to the output
  20. source.getChars(0, actual, results, 0);
  21. // duplicate the previously populated chars as often as needed.
  22. int currentpos = actual;
  23. while (currentpos < size) {
  24. // copy as much as you can, limited by the available space to copy to.
  25. System.arraycopy(results, 0, results, currentpos, Math.min(currentpos, size - currentpos));
  26. // double the current position
  27. currentpos <<= 1;
  28. System.out.println("Currently writing to " + currentpos);
  29. }
  30. return new String(results);
  31. }
  32. public static void main (String[] args) throws java.lang.Exception
  33. {
  34. System.out.println(duplicatePrefix("Hello World", 5, 10));
  35. }
  36. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
Currently writing to 10
Currently writing to 20
Currently writing to 40
Currently writing to 80
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello