WAP For Removing White Spaces In A String

Removing White Spaces In A String: This Java program aims to remove white spaces from a given input string and display the resulting string. It uses the Scanner class to read the input string from the user and then utilizes the replaceAll() method of the String class to eliminate all white spaces from the input.

Removing White Spaces In A String

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms93 
{
	//Write a program for removing white spaces in a String.
	public static void main(String[] args) 
	{
		// create an object of Scanner
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the string");

		// take the input
		String input = sc.nextLine();
		System.out.println("Original String: " + input);

		// remove white spaces
		input = input.replaceAll("\\s", "");
		System.out.println("Final String: " + input);
		sc.close();
	}
}

Output

Enter the string
so ft war e
Original String: so ft war e
After Removing the Spaces: software

Alternative Way 1:

This Java program aims to remove white spaces from a given input string and display the resulting string. It uses a combination of the Scanner class to read the input and manual character processing to eliminate white spaces.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms93_1 
{
	//Write a program for removing white spaces in a String.
	public static void main(String[] args) 
	{
		// create an object of Scanner
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the string");

		// take the input
		String str = sc.nextLine();
		System.out.println("Original String: " + str);

		char[] strArray = str.toCharArray();  
		StringBuffer stringBuffer = new StringBuffer();  
		for (int i = 0; i < strArray.length; i++) 
		{  
			if ((strArray[i] != ' ') && (strArray[i] != '\t')) 
			{  
				stringBuffer.append(strArray[i]);  
			}  
		}  
		String noSpaceStr2 = stringBuffer.toString();  
		System.out.println("After Removing the Spaces: "+noSpaceStr2);  
	}
}

Output

Enter the string
so ft ware
Original String: so ft ware
After Removing the Spaces: software

Alternative Number 2:

This Java program aims to remove white spaces (spaces and tabs) from a given input string and display the resulting string. It utilizes the Scanner class to read user input from the console and then manually processes each character to eliminate white spaces.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms93_1 
{
	//Write a program for removing white spaces in a String.
	public static void main(String[] args) 
	{
		// create an object of Scanner
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the string");

		// take the input
		String str = sc.nextLine();
		System.out.println("Original String: " + str);

		char[] strArray = str.toCharArray();  
		StringBuffer stringBuffer = new StringBuffer();  
		for (int i = 0; i < strArray.length; i++) 
		{  
			if ((strArray[i] != ' ') && (strArray[i] != '\t')) 
			{  
				stringBuffer.append(strArray[i]);  
			}  
		}  
		String noSpaceStr2 = stringBuffer.toString();  
		System.out.println("After Removing the Spaces: "+noSpaceStr2);  
	}
}

Output

Enter the string
so ft ware
Original String: so ft ware
After Removing the Spaces: software

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment