In this article, we will discuss the concept of Java program to count the total words in the given string
In this post, we are going to learn how to count the total number of words in the given string in Java programming language
The program allows the user to enter a string thereafter It counts the total number of words of the given string using for loop in Java language
Program 1
import java.util.Scanner; public class CountWordsOfString{ public static void main(String args[]){ String str; char ch; //variable declaration int count=0;//word counter Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String to count words "); str=scan.nextLine(); for(int i=0; i<str.length(); i++){ ch=str.charAt(i); if (ch==' ') count++;//word counter incremented by 1 } count=count+1; System.out.println("Total number of words: "+count); } }
When the above code is executed it produces the following result
Enter the String to count words Java is the best language for programmers Total number of words: 7
Approach
The program allows the user to enter a string thereafter It counts the words of the string using while loop in Java language
Program 2
import java.util.Scanner; public class CountWordsOfString1{ public static void main(String args[]){ String str; char ch; //variable declaration int count=0;//word counter Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String for count words "); str=scan.nextLine(); int i=0; while(i<str.length()){ ch=str.charAt(i); if (ch==' ') count++;//word counter incremented by 1 i++;//i icremented by 1 } count=count+1; System.out.println("Total number of words: "+count); } }
When the above code is executed it produces the following result
Enter the String for count words Java and c++ are OOP language Total number of words: 6
Approach
The program allows the user to enter a string and thereafter It counts the words of the string using do-while loop in Java language
Program 2
import java.util.Scanner; public class CountWordsOfString2{ public static void main(String args[]){ String str; char ch; //variable declaration int count=0;//word counter Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String for count words "); str=scan.nextLine(); int i=0; do{ ch=str.charAt(i); if (ch==' ') count++;//word counter incremented by 1 i++; }while(i<str.length()); count=count+1; System.out.println("Total number of words: "+count); } }
When the above code is executed it produces the following result
Enter the String for count words Java is fully OOP language Total number of words: 5
Approach
Suggested for you
do-while loop in Java language
Similar post
Java program to count the total number of characters in the given string
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Python program to count the total number of words in the given string
C program to count the total number of words in the given string
C++ program to count the total number of words in the given string
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…