import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
String ret
= wrapText
("a\nme",
4); }
public static String wrapText
(String text,
int maxCharsPerLine
) { // check for null or empty input strings
if (text.length() == 0 || text == null) {
return "";
}
StringBuilder ret_string = new StringBuilder();
int currentLength = 0, index = 0;
char currentLetter = text.charAt(index);
int textLength = text.length();
while (currentLength <= maxCharsPerLine && index < textLength) {
System.
out.
println("=============================================="); System.
out.
println("Current Length: " + currentLength
); System.
out.
println("Index: " + index
);
currentLetter = text.charAt(index);
System.
out.
println("Letter:" + currentLetter
);
// if we're at the end, we should start new line
if (Character.
isLetter(currentLetter
) && currentLength
== maxCharsPerLine
) { System.
out.
println("end of line (1)");
temp
+= String.
valueOf(currentLetter
); int wordLength = temp.length()-1;
char t = text.charAt(wordLength);
System.
out.
println("wordLength: " + wordLength
); System.
out.
println("we're working on: " + cpy
);
StringBuilder word = new StringBuilder();
// work backwards: delete from temp and add to word
t = cpy.charAt(wordLength);
word.append(t);
// delete this character from the temp String
temp = temp.substring(0, temp.length()-1);
wordLength--;
}
// char t is prolly a newline or space now so we insert a newline
ret_string.append(temp);
ret_string.append("\n");
temp = temp.trim();
System.
out.
println("new temp: " + temp
); currentLength = temp.length();
while (!Character.
isLetter(text.
charAt(wordLength
))) { wordLength++;
}
wordLength = index;
index = wordLength;
System.
out.
println("now the index is: " + index
); System.
out.
println("now the length is: " + currentLength
);
// reset temp and currentLength to start a new line
} else if (currentLetter == '\n') {
System.
out.
println("newline"); temp
+= String.
valueOf(currentLetter
); ret_string.append(temp);
temp = "";
currentLength = 0;
// Spaces cannot start a line
} else if (currentLetter == ' ' && currentLength == 0) {
System.
out.
println("space cannot start line"); index++;
continue;
// Spaces cannot end a line
} else if (currentLetter == ' ' && currentLength == maxCharsPerLine) {
System.
out.
println("space cannot end line"); ret_string.append(temp);
ret_string.append("\n");
temp = "";
currentLength = 0;
// only one space between words
} else if (currentLetter == ' ' && text.charAt(index-1) == ' ') {
System.
out.
println("only one space bw words"); index++;
continue;
// append character to temp string
} else {
temp
+= String.
valueOf(currentLetter
); System.
out.
println("Appending to temp:" + temp
); currentLength++;
}
System.
out.
println("current final string:\n" + ret_string
); index++;
}
// finished with entire string
if (temp.length() != 0) {
System.
out.
println("final temp: " + temp
); ret_string.append(temp);
}
return ret_string.toString();
}
}