fork download
  1. class VerticalWords
  2. {
  3. public static void toVerticalWords(String str){
  4.  
  5. //split the words by whitespace
  6. String[] strArr = str.split("\\s");
  7. int maxWordLen = 0;
  8.  
  9. //get the longest word length
  10. for(String strTemp : strArr) {
  11. if(strTemp.length() > maxWordLen)
  12. maxWordLen = strTemp.length();
  13. }
  14.  
  15. //make a matrix of the words with each characetr in an array block
  16. char[][] charArr = new char[strArr.length][maxWordLen];
  17. for(int i=0; i<strArr.length; i++) {
  18. int j=0;
  19. for(char ch : strArr[i].toCharArray()){
  20. charArr[i][j] = ch;
  21. j++;
  22. }
  23. }
  24.  
  25. //print the vertical word pattern
  26. for(int j=0; j<maxWordLen; j++) {
  27. for(int i=0; i<strArr.length; i++) {
  28. if (i!=0)
  29. System.out.print(" ");
  30. System.out.print(charArr[i][j]);
  31. }
  32.  
  33. System.out.println();
  34. }
  35. }
  36.  
  37. public static void main(String[] args)
  38. {
  39. toVerticalWords("Hello Jack the Magnificient");
  40. }
  41. }
  42.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
H J t M
e a h a
l c e g
l k  n
o   i
   f
   i
   c
   i
   e
   n
   t