lundi 5 octobre 2020

How to implements pattern for below logic?

I tried with

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 

But I need

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

flights
flights from
flights from New
flights from New York
flights from New York to
flights from New York to United
flights from New York to United Kingdom

from
from New
from New York
from New York to
from New York to United
from New York to United Kingdom

New
New York
New York to
New York to United
New York to United Kingdom

............

Aucun commentaire:

Enregistrer un commentaire