In this article, we will discuss the concept of the Program to count the white spaces of the given string in Java
In this post, we are going to learn how to count the number of white space in the given string in Java programming language
The program allows the user to enter a String and then it counts the white space in the given string using for loop in Java programing language
Program 1
//program to count te space of the given string import java.util.Scanner; public class Count_space{ public static void main(String args[]){ //variable declaration String str; int space=0; //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(); i++){ char ch=str.charAt(i); if(ch==' '){ space++; } } System.out.println("Total white space : "+space); } }
When the above code is executed, it produces the following result
Enter the string code4coding.com website provides Java tutorials for beginners Total white space: 6
The program allows the user to enter a String and then it counts the white space in the given string using while loop in Java programing language
Program 2
//program to count te space of the given string import java.util.Scanner; public class Count_space1{ public static void main(String args[]){ //variable declaration String str; int space=0; //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()){ char ch=str.charAt(i); if(ch==' '){ space++; } i++; } System.out.println("Total white space : "+space); } }
When the above code is executed, it produces the following result
Enter the string Java C C++ Python tutorials Total white space: 4
The program allows the user to enter a String and then it counts the white space in the given string using do-while loop in Java programing language
Program 3
//program to count te space of the given string import java.util.Scanner; public class Count_space2{ public static void main(String args[]){ //variable declaration String str; int space=0; //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(ch==' '){ space++; } i++; }while(i<str.length()); System.out.println("Total white space : "+space); } }
When the above code is executed, it produces the following result
Enter the string code for coding Total white space: 2
Approaches
Suggested for you
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 vowels and consonants digits special characters and spaces
C program to count the total number of vowels and consonants digits special characters and spaces
C++ program to count the total number of vowels and consonants digits special characters and spaces
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,…