Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java word count

package com.company;
import java.util.*;

public class Main{
    public static int countWords(String s)
    {
        int count=0;

        char ch[]= new char[s.length()];
        for(int i=0;i<s.length();i++)
        {
            ch[i]= s.charAt(i);
            if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
                count++;
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Give word:");
        String word = in.nextLine();
        System.out.println(countWords(word) + " words.");
    }
}
 
PREVIOUS NEXT
Tagged: #java #word #count
ADD COMMENT
Topic
Name
4+2 =