Answers for "Write a function countWords() to count the numbers of words in a string."

1

how to count number of words in a string

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);
Posted by: Guest on June-06-2020

Code answers related to "Write a function countWords() to count the numbers of words in a string."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language