,

Java Program To Remove Extra Blank Spaces From A String

In this article, I have shown a Java program to remove duplicate/extra or repeated blank spaces from a String inputted by the user.

The main logic of this program is to skip all the subsequent blank spaces that appear after a blank space until a non-blank space character is found. I have also included comments, variable description and algorithm along with this Java program.

CODE

import java.util.Scanner;
class RemoveExtraSpace{ //starting of class
    public static void main(String[] args){ //starting of the main function
        Scanner sc=new Scanner(System.in); //declaring an object of the Scanner class
        String input,output=""; 
        char c;
        int i,len;
        System.out.println("ENTER A SENTENCE.");
        input=sc.nextLine().trim(); //accepting the input
        len=input.length();
        for(i=0;i<len;i++){ //outer for loop to traverse each character
            c=input.charAt(i);
            output+=c;
            if(c==' '){
                while(input.charAt(i)==' '){ //inner while loop to skip through repeated spaces
                    i++;
                } //end of while loop
                i--; //decrementing i by 1 so that the immediate character after space is taken
            }
        } //end of for loop
        System.out.println(output); //printing the result
    } //closing of main function
} //closing of class

DRY RUN

Let us understand the code with the help of an example.

Suppose, the inputted String is “JAVA(b)(b)(b)(b)PROGRAM(b)”. Where (b) means blank space.

First of all, this gets trimmed using the .trim() function and the last extra blank space is removed. Thus, we get “JAVA(b)(b)(b)(b)PROGRAM”

Now we enter the loop and start extracting each and every character and add them to the output String if they are not a blank space. Thus, we add everything till “JAVA”, we also add the first blank space. Thus, the output variable holds “JAVA “. Now we skip through the following 3 blank spaces using the while loop.




After the ending of the while loop, the value of i points at the index of “R”, so we decrease the value of i by 1.

Then, we go on extracting and adding the characters again, and are finally left with “JAVA PROGRAM”. Which is the required output.

Variable Description

VARIABLE NAMEDATA TYPEPURPOSE
inputStringTo store the String inputted by the user.
outputStringTo store the result after removing the extra blank spaces.
ccharTo extract one character at a time from the String.
iintUsed as the control variable of for loop.
lenintUsed to store the length of the String inputted by the user.

Algorithm

  1. START.
  2. INCLUDE THE Scanner class FROM THE java.util package.
  3. DEFINE class RemoveExtraSpace.
  4. DEFINE THE main() method.
  5. DECLARE AN OBJECT “sc” OF THE Scanner class WITH (System.in) AS THE PARAMETER.
  6. DECLARE String VARIABLES (input,output). SET output=””.
  7. DECLARE char VARIABLE (c).
  8. DECLARE int VARIABLES (i,len).
  9. ASK THE USER TO ENTER A STRING.
  10. STORE THE USER’S INPUT THE VARIABLE ‘input’ AFTER TRIMMING THE BLANK SPACES AT THE END AND AT THE BEGINNING OF THE STRING.
  11. STORE THE LENGTH OF THE INPUTTED STRING IN A VARIABLE ‘len’.
  12. REPEAT FOR I=0 TO len
    12.1. EXTRACTING A CHARACTER FROM input FROM THE ith POSITION AND STORING IT IN THE VARIABLE c.
    12.2. CONCATINATING c WITH THE output.
    12.3. CHECK IF(c IS A BLANK SPACE) THEN
    12.3.1. REPEAT WHILE(input.charAt(i)==’ ‘)
    12.3.1.1. INCREMENT THE VALUE OF i BY 1.
    12.3.2. END OF THE WHILE LOOP.
    12.3.3. DECREMENT THE VALUE OF i BY 1.
    12.4. END OF THE IF BLOCK.
  13. END OF FOR LOOP.
  14. DISPLAY THE OUTPUT.
  15. END OF THE main() METHOD.
  16. END OF CLASS.
  17. STOP.

If you have any confusion regarding this program, please do not hesitate to comment down below. I will try my best to clear all your doubts.

If you have found this article helpful, kindly share it with your friends who might find it beneficial as well.




Have a nice day!

About Anirban Roy

Anirban Roy is the founder of TechRBun. He is a Computer Geek. He likes to share his knowledge about PC, Mobiles and Blogging. He studies in class XII and when he is not studying, he can always be found tweaking his PC or surfing the web on his mobile phone. He loves music and literature too!

Leave a Comment