806
Table of Contents
In this article, you’ll learn to create a program in java, python and c++ to Remove All Spaces from a String.
Problem Statement:
Given a string, remove all spaces from the string and return it.
Example 1:
Input: “This is a string”
Output: ThisisaString
Example 2:
Input: ” ”
Output: (empty string)
Python Program to Remove All Whitespaces from a String
def remove_spaces(text):
"""Removes spaces from a given string.
Args:
text: The input string.
Returns:
The string with all spaces removed.
"""
return ''.join(text.split())
# Example usage
text = "This is a string"
result = remove_spaces(text)
print(result) # Output: ThisisaString
Explanation:
def remove_spaces(text):: This defines a function namedremove_spacesthat takes a stringtextas input.return ''.join(text.split()):text.split(): Splits the input stringtextbased on spaces, creating a list of words.''.join(): Joins the elements in the list (words) back into a single string with an empty string separator (''), effectively removing spaces.
Java Program to Remove All Whitespaces from a String
public class RemoveSpaces {
public static String removeSpaces(String text) {
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
if (c != ' ') {
sb.append(c);
}
}
return sb.toString();
}
public static void main(String[] args) {
String text = "This is a string";
String result = removeSpaces(text);
System.out.println(result); // Output: ThisisaString
}
}
Explanation:
public class RemoveSpaces {...}: Defines a class namedRemoveSpaces.public static String removeSpaces(String text): Defines a static methodremoveSpacesthat takes a stringtextas input and returns a string.StringBuilder sb = new StringBuilder();: Creates aStringBuilderobjectsbto efficiently build the new string.for (char c : text.toCharArray()) {...}: Iterates through each charactercin the character array obtained fromtext.toCharArray().if (c != ' ') { sb.append(c); }: If the charactercis not a space, it’s appended to theStringBuilderobjectsb.return sb.toString();: Returns the final string built insbafter iterating through all characters.
C++ Program to Remove All Whitespaces from a String
#include <iostream>
#include <string>
using namespace std;
string removeSpaces(const string& text) {
string result;
for (char c : text) {
if (c != ' ') {
result.push_back(c);
}
}
return result;
}
int main() {
string text = "This is a string";
string result = removeSpaces(text);
cout << result << endl; // Output: ThisisaString
return 0;
}
Explanation:
#include <iostream> #include <string>: Includes necessary header files for input/output and string manipulation.string removeSpaces(const std::string& text): Defines a functionremoveSpacesthat takes a constant reference to a stringtextas input and returns a string.string result;: Declares an empty stringresultto store the output.for (char c : text) {...}: Similar to Java, iterates through each charactercin the input string using a range-based for loop.if (c != ' ') { result.push_back(c); }: If the charactercis not a space, it’s pushed back to the