Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

https://java2blog.com/java-program-count-number-words-string/

 
package org.arpit.java2blog.java8;
 
public class CountNumberOfWordsInStringMain {
 
    public static void main(String[] args) {
        String str = "welcome to java   tutorial on Java2blog";
 
        int count = 1;
 
        for (int i = 0; i < str.length() - 1; i++)
        {
            if ((str.charAt(i) == ' ') && (str.charAt(i + 1) != ' '))
            {
                count++;
            }
        }
        System.out.println("Number of words in a string : " + count);
    }
}
 
Source by java2blog.com #
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
5+6 =