+ 2

Can anyone tell me how to output this?

Input - CAT BAT VIN Output-CBV AAI TTN

19th May 2021, 6:57 PM
Atul [Inactive]
13 Answers
19th May 2021, 8:02 PM
A͢J
A͢J - avatar
+ 4
https://code.sololearn.com/c4xfOL3p0xGl/?ref=app
19th May 2021, 6:57 PM
Atul [Inactive]
+ 4
I would say two loops are enough. The outer loop is for the line break. The inner loop is for printing the lines. Let't take the first output CBV C = arr[0].charAt(0) B = arr[1].charAt(0) V = arr[2].charAt(0) Do you see the pattern? Only the index of arr is changing. The index of the char not. Now compare it with your two variables i (from outer loop) and j (from inner loop). Then you should know which variable is for getting the correct index of arr and the correct index for charAt()
19th May 2021, 8:00 PM
Denise Roßberg
Denise Roßberg - avatar
19th May 2021, 8:23 PM
Atul [Inactive]
+ 4
🅰🅹 🅐🅝🅐🅝🅣 I am a beginner I have not started lambda functions. Can I save your code so that it can be helpful in future?
20th May 2021, 4:19 AM
Atul [Inactive]
+ 4
Atul Yes you can
20th May 2021, 4:21 AM
A͢J
A͢J - avatar
+ 3
Atul Just check code again. i have resolved permanently I think. I just took max string length in String array and used that max length for outer loop and used string array length for inner loop. Now you can put any String it will work fine.
19th May 2021, 9:15 PM
A͢J
A͢J - avatar
+ 2
19th May 2021, 8:23 PM
Atul [Inactive]
+ 1
Use System.out.print(t.charAt(k)); and Put System.out.println(); after inner loop exit; edit: simple way print arr[j].charAt(i); j from inner loop, I is outer loop...
19th May 2021, 7:59 PM
Jayakrishna 🇮🇳
+ 1
But what if the Input is MAN BY DORM?
19th May 2021, 8:47 PM
Atul [Inactive]
+ 1
Atul There would be Exception IndexOfBound exception because each string size is not equal so you have to take care of it
19th May 2021, 8:50 PM
A͢J
A͢J - avatar
+ 1
public class Program { public static void main(String[] args) { String n="MAN BY DORM" ; String arr[]=n.split(" "); // String t=""; for(int i=0;i<arr.length;i++){ // t=arr[i]; for(int k=0;k<arr.length;k++){ System.out.print(arr[k].charAt(i)); } System.out.println(); } } } // I have made the loop smaller than array but if no use
19th May 2021, 8:51 PM
Atul [Inactive]
+ 1
🅰🅹 🅐🅝🅐🅝🅣 So how to get the Output as MBD AYO N R M ?? CAN I use anything null or something?
19th May 2021, 8:58 PM
Atul [Inactive]