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
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
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
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
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 code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…