JAVA PROGRAM TO PRINT THE WORDS OF A STRING

 

 

To  print  the  words  of  a   string  in  Java , the  best  approach  is  to   break  the  string  into   an  array  using  the  split()  method  and  then  iterate  through the  results.

 

 

 

 

public  class   PrintWords {

   public  static  void   main(String  args[] ) {

    String   str=“Gift  is  precious”;

   String[ ]   words=str.split(“\\s+”);

   for(String  word :  words ) {

      System.out.println(word);

  }   

    }

}

 

 

     

 

 

 

 

OUTPUT:

 

Gift

is

precious

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *