Answers for "Given a string str[], WAP to: a. Count the number of words in the 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 "Given a string str[], WAP to: a. Count the number of words in the string."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language