String

Count words,characters and space in Java language

Count words, characters and space in Java language

In this article, we will discuss the concept of the count words, characters and space of a given string in Java language

In this post, we are going to learn how to count the total number of words character and Space of the given string in Java programming language

Count words, characters, space

Java code to count words characters and space of a string

Find words, characters and space using for loop

The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using for loop in Java programing language

Program 1

//program to count Alphabets,number,special character and space
import java.util.Scanner;
public class CountWordsCharactersSpace{
public static void main(String args[]){
//variable declaration
    String str;
int words=1,characters=1;
//vriable declaration and initialization
 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String  ");
str=scan.nextLine();

for(int i=0; i<str.length()-1; i++){
char ch=str.charAt(i);
if(str.charAt(i)==' ' && str.charAt(i+1)!=' '){//count words
    words++;
}

else if(str.charAt(i)!=' '){//count characters
    characters++;
}
}
System.out.println("\nTotal words: "+words);//display words
System.out.println("Total characters: "+characters);//display characters
System.out.println("Space: "+(words-1));//display space
}
}

When the above code is executed, it produces the following result

Enter the String
This is my Java

Totals words: 4
Total characters: 12
Space: 3

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as words=1, characters=1;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A for loop is used to count every total (words,characters,spaces)of the given string.
  6. It is initialized as i=0, 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(str.charAt(i)==’ ‘ && str.charAt(i+1)!=’ ‘), if it is true,  The words becomes words + 1( words = words +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The characters becomes Characters+1(characters=characters+1);
  9. Finally, the program displays the total number of the words, characters and Space of the given string

 

 

Find words, characters and space using while loop

The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using while loop in Java programing language

Program 2

//program to count Alphabets,number,special character and space
import java.util.Scanner;
public class CountWordsCharactersSpace1{
public static void main(String args[]){
//variable declaration
    String str;
int words=1,characters=1;
//vriable declaration and initialization
 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String  ");
str=scan.nextLine();

int i=0;
while(i<str.length()-1){
char ch=str.charAt(i);
if(str.charAt(i)==' ' && str.charAt(i+1)!=' '){
    words++;
}

else if(str.charAt(i)!=' '){
    characters++;
}
 i++;
}
System.out.println("\nTotal words: "+words);
System.out.println("Total characters: "+characters);
System.out.println("Space: "+(words-1));
}
}

When the above code is executed, it produces the following result

Enter the String
Java is an OOP language


Totals words: 5
Total characters: 19
Space: 4

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as words=1, characters=1;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A wile loop is used to count every total of the given string.
  6. It is initialized as i=0, 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(str.charAt(i)==’ ‘ && str.charAt(i+1)!=’ ‘), if it is true,  The words becomes words + 1( words = words +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The characters becomes Characters+1(characters=characters+1);
  9. Finally, the program displays the total number of the words, characters and Space of the given string

 

Find words, characters and space using do-while loop

The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using do-while loop in Java programing language

Program 3

//program to count Alphabets,number,special character and space
import java.util.Scanner;
public class CountWordsCharactersSpace2{
public static void main(String args[]){
//variable declaration
    String str;
int words=1,characters=1;
//vriable declaration and initialization
 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String  ");
str=scan.nextLine();

int i=0;
do{
char ch=str.charAt(i);
if(str.charAt(i)==' ' && str.charAt(i+1)!=' '){//check words
    words++;
}

else if(str.charAt(i)!=' '){ //check characters
    characters++;
}
 i++;
}while(i<str.length()-1);
System.out.println("\nTotal words: "+words);//display total number of words
System.out.println("Total characters: "+characters); //display total number of characters
System.out.println("Space: "+(words-1));//display total number of spaces
}
}

When the above code is executed, it produces the following result

Enter the String
code4coding.com provides the best Java tutorials

Totals words: 6
Total characters: 43
Space: 5

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as words=1, characters=1;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A do-while loop is used to count every total of the given string.
  6. It is initialized as i=0, 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(str.charAt(i)==’ ‘ && str.charAt(i+1)!=’ ‘), if it is true,  The words becomes words + 1( words = words +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The characters becomes Characters+1(characters=characters+1);
  9. Finally, the program displays the total number of the words, characters and Space of the given string

 

 

Suggested for you

for loop in Java language

while loop in Java language

do-while loop in Java language

 

Similar post

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

 

Java program to count the total number of  upper case lower case numeric and special character

C program to count the total number of  upper case lower case numeric and special character

Python program to count the total number of  upper case lower case numeric and special character

C++ program to count the total number of  upper case lower case numeric and special character

Python program to Count words, characters and space of a string
C++ program: Count word, characters and space of a 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

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

8 hours ago

How to find reverse number using method in Java

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

3 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