
how we conunt the no. of words in java without using in pre defined method in java

Here is a java code that counts the number of words in a string.
import java.util.*;
class CountWords{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter string: ");
String st=input.nextLine();
String arr[]=st.split(" ");
System.out.println("Number of words are: "+arr.length);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.