Check out example codes for "how to count number of words in a string". It will help you in understanding the concepts better.

Code Example 1

String name = "Carmen is a fantastic play"; //arbitrary sentence
        
        int numWords = (name.split("\\s+")).length; //split string based on whitespace
                                                //split returns array - find legth of array
        
        System.out.println(numWords);

Learn ReactJs, React Native from akashmittal.com