Categories: Frequently Asked Java Programs In Interview

Frequently Asked Java Program 23: Java Program to Remove Extra WhiteSpace Between Words In Given String

Problem Statement:

Remove extra white spaces between words. Only one white space is allowed between words. If more than one, remove it. If given string has leading or trailing white spaces, remove them as well. Do not use trim method. Write your own logic.

Example:

You entered with spaces: ”       Make       Selenium         Easy       ”
String after removing extra white space: “Make Selenium Easy”

This program was asked in Sapient.

Problem Solution:

  1. We have trim method which only removes leading and trailing whitespace/s of given string. We do not have any readymade methods to remove white spaces between words.
  2. We need to create a char array from given string which we need to iterate.
  3. If char at given index is a non space char, append it to result string.
  4. If char at given index is a white space, check if resultant string is zero in length. If length of resultant string is zero, go to next iteration. We need to go to next iteration as we do not want any leading white spaces in our resultant string. If length of resultant string is not zero, check if next index char is non space. If it is non space, append char to resultant string else to next iteration.

 

Java Program:

 

package StringPrograms;

import java.util.Scanner;

public class RemoveExtraWhiteSpacesBetweenWords {

        public static String removeExtraWhitespace(String str) {
                // Convert given string to a char array
                char[] c = str.toCharArray();

                // Output string variable
                String stringWithoutExtraWhitespaces = "";

                // For loop
                for (int i = 0; i 

Output:

Please enter the string to trim extra whitespaces:
       Make      Selenium       Easy    
You entered with spaces: "       Make      Selenium       Easy    "
String after removing extra white space: "Make Selenium Easy"
===================================================================
Please enter the string to trim extra whitespaces:
Make       selenium      easy
You entered with spaces: "Make       selenium      easy"
String after removing extra white space: "Make selenium easy"
====================================================================

#HappyCoding

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning




              
Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Recent Posts

TestNG Tutorials 58: DataProvider in TestNG – Running Test for Specific Set of Data Provided by DataProvider Method – Dataprovider With Indices

By default a Test method is executed for all data set returned by DataProvider. The reason behind this is that…

2 hours ago

TestNG Tutorials 57: DataProvider in TestNG – Passing External Data File Name to DataProvider Method Using TestNG xml

In last post, we have learnt how can we make parameterized DataProvider method in TestNG to provide test data to…

5 hours ago

TestNG Tutorials 56: DataProvider in TestNG – Parameterizing DataProvider Method to Provide Data to Multiple Test Methods

"DataProvider" is an important feature provided by TestNG to provide required test data to test methods to run on. We…

6 hours ago

Frequently Asked Java Program 22: Java Program to Trim Whitespace From String Without Using Trim Method

Hello Folks, This interview question was asked in Deloitte.  Write a java program to Trim a given string without using…

6 days ago

TestNG Tutorials 55: DataProvider in TestNG – Lazy Initialisation of DataProvider Method – Use of Iterator Return Type

Hello, DataProvider is an important functionality provided by TestNG to achieve Data driven testing or providing a set of different…

2 weeks ago

TestNG Tutorials 54: DataProvider in TestNG – Implementing Object Oriented Concept “Encapsulation” with DataProvider Method

Hello Folks, As part of ongoing series on DataProvider, we will learn "How does Encapsulation ( in OOPS concept) help in…

2 weeks ago