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

Step-by-Step Explanation:

  • The program starts by importing the required classes, including Scanner.
  • It defines a public class named InterviewPrograms93.
  • The main method serves as the entry point of the program.
  • It creates a new Scanner object (sc) to read user input from the console.
  • The user is prompted to enter a string using System.out.println(“Enter the string”);.
  • The input string is read from the user using String input = sc.nextLine();.
  • The program prints the original input string using System.out.println(“Original String: ” + input);.
  • The program then removes all white spaces from the input string using input = input.replaceAll(“\\s”, “”);. The replaceAll() method replaces all occurrences of whitespace characters (denoted by “\\s”) with an empty string, effectively removing them from the input.
  • Finally, the program prints the modified string with white spaces removed using System.out.println(“Final String: ” + input);.
  • The Scanner is closed using sc.close() to release resources.

Summary: The Java program allows users to input a string and then removes all white spaces from the input. The original input string and the modified string (with white spaces removed) are displayed on the console. This program is helpful for beginners to understand how to read user input, use the String class’s replaceAll() method for string manipulation, and achieve the task of removing white spaces from a string in Java.

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 from the input.

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

Step-by-Step Explanation:

  • The program starts by importing the required class, Scanner.
  • It defines a public class named InterviewPrograms93_1.
  • The main method serves as the entry point of the program.
  • It creates a new Scanner object (sc) to read user input from the console.
  • The user is prompted to enter a string using System.out.println(“Enter the string”);.
  • The input string is read from the user using String str = sc.nextLine();.
  • The program prints the original input string using System.out.println(“Original String: ” + str);.
  • It converts the input string into a character array (strArray) to process individual characters.
  • A StringBuffer named stringBuffer is created to store characters without white spaces.
  • A loop iterates through each character in strArray.
  • If the character is not a white space or a tab character, it appends the character to stringBuffer using stringBuffer.append(strArray[i]);.
  • After processing all characters, the stringBuffer is converted back to a string (noSpaceStr2) using stringBuffer.toString().
  • The program prints the modified string with white spaces removed using System.out.println(“After Removing the Spaces: ” + noSpaceStr2);.
  • The Scanner is closed using sc.close() to release resources.

Summary: The Java program allows users to input a string and then manually processes each character to remove white spaces. The original input string and the modified string (with white spaces removed) are displayed on the console.

This program is helpful for beginners to understand how to read user input, work with character arrays and StringBuffer, and achieve the task of removing white spaces from a string in Java without using the String class’s built-in methods like replaceAll().

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

Step-by-Step Explanation:

  • The program starts by importing the required class, Scanner.
  • It defines a public class named InterviewPrograms93_1.
  • The main method serves as the entry point of the program.
  • It creates a new Scanner object (sc) to read user input from the console.
  • The user is prompted to enter a string using System.out.println(“Enter the string”);.
  • The input string is read from the user using String str = sc.nextLine();.
  • The program prints the original input string using System.out.println(“Original String: ” + str);.
  • It converts the input string into a character array (strArray) to process individual characters.
  • A StringBuffer named stringBuffer is created to store characters without white spaces.
  • A loop iterates through each character in strArray.
  • If the character is not a space or a tab character, it appends the character to stringBuffer using stringBuffer.append(strArray[i]);.
  • After processing all characters, the stringBuffer is converted back to a string (noSpaceStr2) using stringBuffer.toString().
  • The program prints the modified string with white spaces removed using System.out.println(“After Removing the Spaces: ” + noSpaceStr2);.
  • The Scanner is closed using sc.close() to release resources.

Summary: The Java program allows users to input a string and then manually processes each character to remove white spaces (spaces and tabs). The original input string and the modified string (with white spaces removed) are displayed on the console.

This program is helpful for beginners to understand how to read user input, work with character arrays and StringBuffer, and achieve the task of removing white spaces from a string in Java without using the String class’s built-in methods like replaceAll(). It demonstrates an alternative approach to remove white spaces by explicitly examining each character and building a new string without white spaces.

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