by Dinesh Thakur Category: Stream

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]);
                 }
        }
}

Split Function in Java with Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles