Given a string s, remove all spaces from the string and return it.
Examples:
Input: s = "g eeks for ge eeks "
Output: "geeksforgeeks"Input: s = "abc d "
Output: "abcd"
Table of Content
[Naive Approach] Using Brute Force - O(n*n) Time and O(1) Space
Traverse the string and whenever a space is found, shift all the following characters one position to the left and reduce the effective length of the string.
#include <iostream>
#include <string>
using namespace std;
string removeSpaces(string s) {
int n = s.length();
for (int i = 0; i < n; i++) {
if (s[i] == ' ') {
// shift all characters left
for (int j = i; j < n - 1; j++) {
s[j] = s[j + 1];
}
n--;
i--;
}
}
// return string up to new length
return s.substr(0, n);
}
int main() {
string s = "g eeks for ge eeks ";
cout << removeSpaces(s);
return 0;
}
#include <stdio.h>
#include <string.h>
char* removeSpaces(char s[]) {
int n = strlen(s);
for (int i = 0; i < n; i++) {
if (s[i] == ' ') {
// shift all characters left
for (int j = i; j < n - 1; j++) {
s[j] = s[j + 1];
}
n--;
i--;
}
}
s[n] = '\0'; // terminate string
return s;
}
int main() {
char s[] = "g eeks for ge eeks ";
printf("%s", removeSpaces(s));
return 0;
}
class GFG {
static String removeSpaces(String s) {
char[] arr = s.toCharArray();
int n = arr.length;
for (int i = 0; i < n; i++) {
if (arr[i] == ' ') {
// shift all characters left
for (int j = i; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
n--;
i--;
}
}
return new String(arr, 0, n); // return valid part
}
public static void main(String[] args) {
String s = "g eeks for ge eeks ";
System.out.print(removeSpaces(s));
}
}
def removeSpaces(s):
res = []
for ch in s:
if ch != ' ':
res.append(ch)
return "".join(res)
if __name__ == "__main__":
s = "g eeks for ge eeks "
print(removeSpaces(s))
using System;
class GFG {
static string RemoveSpaces(string s) {
char[] arr = s.ToCharArray();
int n = arr.Length;
for (int i = 0; i < n; i++) {
if (arr[i] == ' ') {
// shift all characters left
for (int j = i; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
n--;
i--;
}
}
return new string(arr, 0, n); // return valid part
}
static void Main() {
string s = "g eeks for ge eeks ";
Console.Write(RemoveSpaces(s));
}
}
function removeSpaces(s) {
s = s.split('');
let n = s.length;
for (let i = 0; i < n; i++) {
if (s[i] === ' ') {
// shift all characters left
for (let j = i; j < n - 1; j++) {
s[j] = s[j + 1];
}
n--;
i--;
}
}
return s.slice(0, n).join(""); // return result
}
// Driver code
let s = "g eeks for ge eeks ";
console.log(removeSpaces(s));
Output
geeksforgeeeks
[Expected Approach] Using Two Pointer - O(n) Time and O(1) Space
Traverse the string using two indices, where one index reads each character and the other writes only non-space characters. Whenever a non-space character is found, place it at the write index and move both pointers accordingly. This way, all non-space characters are shifted forward without using extra space.
#include <iostream>
#include <string>
using namespace std;
string removeSpaces(string s) {
int n = s.length();
int j = 0; // write index
for (int i = 0; i < n; i++) {
if (s[i] != ' ') {
s[j++] = s[i];
}
}
return s.substr(0, j);
}
int main() {
string s = "g eeks for ge eeks ";
cout << removeSpaces(s);
return 0;
}
#include <stdio.h>
#include <string.h>
char* removeSpaces(char s[]) {
int j = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] != ' ') {
s[j++] = s[i];
}
}
s[j] = '\0';
return s;
}
int main() {
char s[] = "g eeks for ge eeks ";
printf("%s", removeSpaces(s));
return 0;
}
class GFG {
static String removeSpaces(String s) {
char[] arr = s.toCharArray();
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != ' ') {
arr[j++] = arr[i];
}
}
return new String(arr, 0, j);
}
public static void main(String[] args) {
String s = "g eeks for ge eeks ";
System.out.print(removeSpaces(s));
}
}
def removeSpaces(s):
res = []
for ch in s:
if ch != ' ':
res.append(ch)
return "".join(res)
if __name__ == "__main__":
s = "g eeks for ge eeks "
print(removeSpaces(s))
using System;
class GFG {
static string RemoveSpaces(string s) {
char[] arr = s.ToCharArray();
int j = 0;
for (int i = 0; i < arr.Length; i++) {
if (arr[i] != ' ') {
arr[j++] = arr[i];
}
}
return new string(arr, 0, j);
}
static void Main() {
string s = "g eeks for ge eeks ";
Console.Write(RemoveSpaces(s));
}
}
function removeSpaces(s) {
let arr = s.split('');
let j = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== ' ') {
arr[j++] = arr[i];
}
}
return arr.slice(0, j).join("");
}
// Driver code
let s = "g eeks for ge eeks ";
console.log(removeSpaces(s));
Output
geeksforgeeeks
Using Built-in functions - O(n) Time and O(1) Space
Use the built-in
remove()function to shift all non-space characters to the front of the string while returning the new logical end. Then useerase()to remove the remaining unwanted part of the string. This avoids manual shifting and processes each character only once.
#include <iostream>
#include <algorithm>
using namespace std;
// Function to remove all spaces
string removeSpaces(string s) {
// move non-space characters to front
auto new_end = remove(s.begin(), s.end(), ' ');
// erase extra part
s.erase(new_end, s.end());
return s;
}
int main() {
string s = "g eeks for ge eeks ";
cout << removeSpaces(s);
return 0;
}
public class GFG {
// Function to remove all spaces
static String removeSpaces(String s) {
// replace all spaces with empty string
s = s.replace(" ", "");
return s;
}
public static void main(String[] args) {
String s = "g eeks for ge eeks ";
System.out.print(removeSpaces(s));
}
}
def removeSpaces(s):
# replace all spaces with empty string
s = s.replace(" ", "")
return s
if __name__ == "__main__":
s = "g eeks for ge eeks "
print(removeSpaces(s), end="")
using System;
class GFG {
// Function to remove all spaces
static string RemoveSpaces(string s) {
// replace all spaces with empty string
s = s.Replace(" ", "");
return s;
}
static void Main() {
string s = "g eeks for ge eeks ";
Console.Write(RemoveSpaces(s));
}
}
function removeSpaces(s) {
return s.replace(/ /g, "");
}
// Driver code
let s = "g eeks for ge eeks ";
console.log(removeSpaces(s));
Output
geeksforgeeeks
note: C does not have a built-in function to remove spaces, so it must be done manually.