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
