1) Iterate through all characters of given string, do following
a) If current character is a space, then move all subsequent
characters one position back and decrease length of the
result string.
Time complexity of above solution is O(n2). A Better Solution can solve it in O(n) time. The idea is to keep track of count of non-space character seen so far.
1) Initialize 'count' = 0 (Count of non-space character seen so far)
2) Iterate through all characters of given string, do following
a) If current character is non-space, then put this character
at index 'count' and increment 'count'
3) Finally, put '\0' at index 'count'
Below is the implementation of above algorithm.
C++
// An efficient C++ program to remove all spaces
// from a string
#include <iostream>
usingnamespacestd;
// Function to remove all spaces from a given string
voidremoveSpaces(char*str)
{
// To keep track of non-space character count
intcount = 0;
// Traverse the given string. If current character
Time complexity of above solution is O(n) and it does only one traversal of string. Another solution suggested by Divyam Madaan is to use predefined functions. Here is the implementation:
C++
// CPP program to Remove spaces
// from a given string
#include <iostream>
#include <algorithm>
usingnamespacestd;
// Function to remove all spaces from a given string
# Python program to Remove spaces from a given string
# Function to remove all spaces from a given string
defremoveSpaces(string):
string =string.replace(' ','')
returnstring
# Driver program
string ="g eeks for ge eeks "
print(removeSpaces(string))
# This code is contributed by Divyam Madaan
C#
// C# program to remove
// all spaces from a string
usingSystem;
classGFG
{
// Function to remove all
// spaces from a given string
staticString removeSpace(String str)
{
str = str.Replace(" ","");
returnstr;
}
// Driver Code
publicstaticvoidMain()
{
String str = "g eeks for ge eeks ";
Console.WriteLine(removeSpace(str));
}
}
// This code is contributed by
// PrinciRaj1992
Javascript
<script>
// javascript program to remove
// all spaces from a string
// Function to remove all
// spaces from a given string
functionremoveSpace( str)
{
str = str.replace(/\s/g,'')
returnstr;
}
// Driver Code
varstr = "g eeks for ge eeks ";
document.write(removeSpace(str));
// This code contributed by aashish1995
</script>
Output
geeksforgeeeks
Another method to solve this problem using predefined STL functions like count() ,remove() ,getline() and resize() is also present. Here is the implementation for the same :
C++
#include <bits/stdc++.h>
usingnamespacestd;
intmain()
{
string s = "g e e k s f o r g e e k s";
cout << "string with spaces is "<< s << endl;
intl = s.length(); // storing the length of the string
intc
= count(s.begin(), s.end(),
' '); // counting the number of whitespaces
remove(s.begin(), s.end(),
' '); // removing all the whitespaces
s.resize(l - c); // resizing the string to l-c
cout << "string without spaces is "<< s << endl;
return0;
}
Output
string with spaces is g e e k s f o r g e e k s
string without spaces is geeksforgeeks
Thanks to Souravi Sarkar for suggesting this problem and initial solution. Java | Removing whitespaces using Regex Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy