-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerticalString.java
More file actions
28 lines (28 loc) · 953 Bytes
/
VerticalString.java
File metadata and controls
28 lines (28 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.*;
public class VerticalString {
public static List<String> printVertically(String s) {
String[] words = s.split(" ");
int mx = 0;
for (int i = 0; i < words.length; ++i)
mx = Math.max(mx, words[i].length());
List<String> ans = new ArrayList<>();
for (int i = 0; i < mx; ++i) {
StringBuilder sb = new StringBuilder();
for (String word : words)
sb.append(i < word.length() ? word.charAt(i) : " ");
while (sb.charAt(sb.length() - 1) == ' ')
sb.deleteCharAt(sb.length() - 1);
ans.add(sb.toString());
}
return ans;
}
public static void main(String[] args){
Scanner s=new Scanner(System.in);
System.out.println("Enter a string");
String str=s.nextLine();
List<String> list=printVertically(str);
for(String i:list){
System.out.println(i);
}
}
}