• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP For Removing White Spaces In A String

WAP For Removing White Spaces In A String

Last Updated on: August 2, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • 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.

    Sort A String According To The Frequency Of Characters
    WAP to Sort A String According To The Frequency Of...
    WAP For Sum Until the Sum Of All Digits is a Single Digit
    WAP For Sum Until the Sum Of All Digits is...
    Java Program Interview Reverse Characters Only From String
    WAP to Reverse Characters Only From String
    WAP to Check Character Sequence Is in Same Order Or Not (isomorphic)
    WAP to Check Character Sequence Is in Same Order Or...
    WAP to Swap Two Adjacent Elements in an Array
    WAP to Swap Two Adjacent Elements in an Array
    Print Character And Count Of a Word
    Write a Java Program[ abbcccdeee –>a1b2c3d1e3]
    WAP to Repeat Each Letter According to its Corresponding Count
    WAP to Print Character by its Corresponding Count
    Duplicate Characters In a String Java
    Find Duplicate Characters In a String Java
    WAP to Verify Balanced Parentheses In Java
    WAP to Verify Balanced Parentheses In Java
    Find the Duplicate Words in a String & File
     Program to Find the Duplicate Words in a String &...

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers