String

Program to count white space of the given string in Java

Program to count white space of the given string

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

Count white space of the given string

Code to find the white space in the string

count the space in the given string using for loop

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

 

count the space in the given string using while loop

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

 

count the space in the given string using  do-while loop

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

  1. Declare a String variable as String str;
  2. Declare and initialize integer variables as space=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A loop(for, while and do-while) is used to count space in 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(ch==’ ‘, when the if statements is true,  The space becomes space + 1( space = space +1);
  8. When it is false, terminates the loop
  9. Finally, the program displays the total number of 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 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

Python program: find the frequency of a character in a string
Count Number of space of the given string in C language
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

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

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago