fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone {
  7. public static void main (String[] args) throws java.lang.Exception {
  8. String ret = wrapText("a\nme", 4);
  9. System.out.println(ret);
  10. }
  11.  
  12. public static String wrapText(String text, int maxCharsPerLine) {
  13. // check for null or empty input strings
  14. if (text.length() == 0 || text == null) {
  15. return "";
  16. }
  17.  
  18. StringBuilder ret_string = new StringBuilder();
  19. String temp = "";
  20.  
  21. int currentLength = 0, index = 0;
  22. char currentLetter = text.charAt(index);
  23. int textLength = text.length();
  24.  
  25. while (currentLength <= maxCharsPerLine && index < textLength) {
  26. System.out.println("==============================================");
  27. System.out.println("Current Length: " + currentLength);
  28. System.out.println("Index: " + index);
  29.  
  30. currentLetter = text.charAt(index);
  31. System.out.println("Letter:" + currentLetter);
  32.  
  33. // if we're at the end, we should start new line
  34. if (Character.isLetter(currentLetter) && currentLength == maxCharsPerLine) {
  35. System.out.println("end of line (1)");
  36.  
  37. temp += String.valueOf(currentLetter);
  38. String cpy = temp;
  39. int wordLength = temp.length()-1;
  40. char t = text.charAt(wordLength);
  41.  
  42. System.out.println("wordLength: " + wordLength);
  43. System.out.println("we're working on: " + cpy);
  44.  
  45. StringBuilder word = new StringBuilder();
  46.  
  47. // work backwards: delete from temp and add to word
  48. while(Character.isLetter(t)) {
  49. t = cpy.charAt(wordLength);
  50. word.append(t);
  51.  
  52. // delete this character from the temp String
  53. temp = temp.substring(0, temp.length()-1);
  54. wordLength--;
  55. }
  56.  
  57. // char t is prolly a newline or space now so we insert a newline
  58. ret_string.append(temp);
  59. ret_string.append("\n");
  60.  
  61. temp = new StringBuffer(word).reverse().toString();
  62.  
  63. temp = temp.trim();
  64. System.out.println("new temp: " + temp);
  65. currentLength = temp.length();
  66.  
  67. while (!Character.isLetter(text.charAt(wordLength))) {
  68. wordLength++;
  69. }
  70.  
  71. wordLength = index;
  72. index = wordLength;
  73. System.out.println("now the index is: " + index);
  74. System.out.println("now the length is: " + currentLength);
  75.  
  76. // reset temp and currentLength to start a new line
  77. } else if (currentLetter == '\n') {
  78. System.out.println("newline");
  79. temp += String.valueOf(currentLetter);
  80. ret_string.append(temp);
  81. temp = "";
  82. currentLength = 0;
  83.  
  84. // Spaces cannot start a line
  85. } else if (currentLetter == ' ' && currentLength == 0) {
  86. System.out.println("space cannot start line");
  87. index++;
  88. continue;
  89.  
  90. // Spaces cannot end a line
  91. } else if (currentLetter == ' ' && currentLength == maxCharsPerLine) {
  92. System.out.println("space cannot end line");
  93. ret_string.append(temp);
  94. ret_string.append("\n");
  95. temp = "";
  96. currentLength = 0;
  97.  
  98. // only one space between words
  99. } else if (currentLetter == ' ' && text.charAt(index-1) == ' ') {
  100. System.out.println("only one space bw words");
  101. index++;
  102. continue;
  103.  
  104. // append character to temp string
  105. } else {
  106. temp += String.valueOf(currentLetter);
  107. System.out.println("Appending to temp:" + temp);
  108. currentLength++;
  109. }
  110. System.out.println("current final string:\n" + ret_string);
  111. index++;
  112. }
  113.  
  114. // finished with entire string
  115. if (temp.length() != 0) {
  116. System.out.println("final temp: " + temp);
  117. ret_string.append(temp);
  118. }
  119. return ret_string.toString();
  120. }
  121. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
==============================================
Current Length: 0
Index: 0
Letter:a
Appending to temp:a
current final string:

==============================================
Current Length: 1
Index: 1
Letter:

newline
current final string:
a

==============================================
Current Length: 0
Index: 2
Letter:m
Appending to temp:m
current final string:
a

==============================================
Current Length: 1
Index: 3
Letter:e
Appending to temp:me
current final string:
a

final temp: me
a
me