Java TechnologyJava Tutorial Java Program to count the number of words in a string by Online Tutorials Library July 22, 2022 77 Write a Java program to count the number of words in a string? File: WordCount .java public class WordCount { static int wordcount(String string) { int count=0; char ch[]= new char[string.length()]; for(int i=0;i<string.length();i++) { ch[i]= string.charAt(i); if( ((i>0)&&(ch[i]!=’ ‘)&&(ch[i-1]==’ ‘)) || ((ch[0]!=’ ‘)&&(i==0)) ) count++; } return count; } public static void main(String[] args) { String string =” India Is My Country”; System.out.println(wordcount(string) + ” words.”); } } Output: 4 words. Next TopicJava String FAQs charAt()examplejavamethodsignaturestring Share 0 FacebookTwitterPinterestEmail previous post Java Integer next post Java program to create a Circular Linked List of N nodes and count the number of nodes You may also like Java Email Validation Java String charAt() method Java String toLowerCase() method Java Collections replaceAll() Method with Examples Java Interpreter Java SHA