I tried with
public static void rightTriangle(int n) { int rows, i, j=0; String s = "Cheap flights from New York to United Kingdom"; String[] arr = s.split("\s"); for (i = 0; i <= arr.length - 1; i++) { for (j = 0; j <= i; j++) System.out.print(arr[j] + " "); System.out.println(""); } }
And I got
Cheap Cheap flights Cheap flights from Cheap flights from New Cheap flights from New York Cheap flights from New York to Cheap flights from New York to United Cheap flights from New York to United Kingdom
I I am I am a I am a girl am am a am a girl a a girl girl
Advertisement
Answer
Here you need to introduce one more for loop to get your desired output.
String s = "Cheap flights from New York to United Kingdom"; String[] arr = s.split("\s"); for (int i = 0; i < arr.Length; i++) { for (int j = i; j < arr.Length; j++) { for (int k = i; k <= j; k++) { System.out.print(arr[k] + " "); } System.out.println(""); } System.out.println(""); }