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
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
- Declare a String variable as String str;
- Declare and initialize integer variables as words=1, characters=1;
- The user asked to enter a string
- The given string is stored in the variable str;
- A for loop is used to count every total (words,characters,spaces)of the given string.
- It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
- 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);
- 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);
- 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
- Declare a String variable as String str;
- Declare and initialize integer variables as words=1, characters=1;
- The user asked to enter a string
- The given string is stored in the variable str;
- A wile loop is used to count every total of the given string.
- It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
- 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);
- 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);
- 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
- Declare a String variable as String str;
- Declare and initialize integer variables as words=1, characters=1;
- The user asked to enter a string
- The given string is stored in the variable str;
- A do-while loop is used to count every total of the given string.
- It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
- 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);
- 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);
- 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