Vertical Text Function in Java
A Challenge from Sloth Bytes
One of my favorite newsletters is Sloth Bytes. Wanted to do this week’s challenge:
Solution:
import java.util.*;
public class VerticalText {
public static List<List<String>> vertical_txt(String input) {
String[] words = input.split(" ");
// Step 1: find max word length
int maxLen = 0;
for (String word : words) {
maxLen = Math.max(maxLen, word.length());
}
List<List<String>> result = new ArrayList<>();
// Step 2: build vertical rows
for (int i = 0; i < maxLen; i++) {
List<String> row = new ArrayList<>();
for (String word : words) {
if (i < word.length()) {
row.add(String.valueOf(word.charAt(i)));
} else {
row.add(" ");
}
}
result.add(row);
}
return result;
}
public static void main(String[] args) {
List<List<String>> output = vertical_txt("Coffee time");
for (List<String> row : output) {
System.out.println(row);
}
}
}CodeHS has a great sandbox for Java, where I tested this code.


