In Split Function Java Example, the string t is split into pieces (wherever space has occurred in it) with the help of split() method and all its pieces are then stored in the string array h. Then, the length of the string h is computed by length() function and the loop is invoked in reverse to display the tokens stored in string array h in reverse order.
import java.io.*;
class SplitFunction
{
public static void main(String args[]) throws IOException
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String t;
int i,n;
System.out.println("Enter a line of text");
t=bf.readLine();
String[] h = t.split(" ");
System.out.println("the words in reverse order are");
n=h.length;
for(i=n-1;i>=0;i--)
{
System.out.println(h[i]);
}
}
}