String

Java program to count the total words in the given string

Java program to count the total words in the given string

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

Count words of the string

Java program to count the total words in the given string

Program to count the total words using for loop

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

  1. Declare two variable(String and character) as String str, char ch;
  2. Declare and initialize an integer variable as int count=0;
  3. The user is asked to enter a string to count words
  4. A for-loop is used to count total words of the given string using the count variable
  5. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  6. Use an if condition to test if(ch==’  ‘), if it is true,  count becomes count + 1(count=count+1)
  7. Finally, the program displays the total number of the words using the count variable

 

Program to count the total words using while loop

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

  1. Declare two variable(String and character) as String str, char ch;
  2. Declare and initialize an integer variable as int count=0;
  3. The user is asked to enter a string to count words
  4. Declare and initialize an integer variable as int i=0;
  5. A while-loop is used to count total words of the given string using the count variable
  6. It checks the condition whether i<str.length(), and executes the loop until the given condition becomes true
  7. Use an if condition to test if(ch==’  ‘), if it is true,  count  becomes count + 1(count=count+1)
  8. i is incremented by 1(i=i+1)
  9. Finally, the program displays the total number of the words using the count variable

Program to count the total words using do-while loop

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

  1. Declare two variable(String and character) as String str, char ch;
  2. Declare and initialize an integer variable as int count=0;
  3. The user is asked to enter a string to count words
  4. Declare and initialize an integer variable as int i=0;
  5. A do-wile loop is used to count total words of the given string using the count variable
  6. It checks the condition whether i<str.length(), and executes the loop until the given condition becomes true
  7. Use an if condition to test if(ch==’  ‘), if it is true,  count becomes count + 1(count=count+1)
  8. i is incremented by 1(i=i+1)
  9. Finally, the program displays the total number of the words using the count variable

 

 

Suggested for you

for loop in Java language

while loop in Java language

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

 

 

Python program to count the total number of characters in the given string
C++ program to count the total number of words in the given string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago