Answers for "substring word count java"

2

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.");
    }
}
Posted by: Guest on June-06-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language