+ 2
Can anyone tell me how to output this?
Input - CAT BAT VIN Output-CBV AAI TTN
13 odpowiedzi
+ 8
Atul
Nice Program:
See this
https://code.sololearn.com/cI60XrM4D1r7/?ref=app
+ 4
https://code.sololearn.com/c4xfOL3p0xGl/?ref=app
+ 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()
+ 4
Denise Roßberg Jayakrishna🇮🇳 sums up that
+ 4
🅰🅹 🅐🅝🅐🅝🅣 I am a beginner I have not started lambda functions. Can I save your code so that it can be helpful in future?
+ 4
Atul Yes you can
+ 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.
+ 2
Jayakrishna🇮🇳 nice logic
+ 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...
+ 1
But what if the Input is MAN BY DORM?
+ 1
Atul
There would be Exception IndexOfBound exception because each string size is not equal so you have to take care of it
+ 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
+ 1
🅰🅹 🅐🅝🅐🅝🅣 So how to get the Output as
MBD
AYO
N R
M
?? CAN I use anything null or something?