C++ Program to remove spaces from a string Last Updated : 03 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Given a string, remove all spaces from it. For example "g e e k" should be converted to "geek" and " g e " should be converted to "ge". The idea is to traverse the string from left to right and ignore spaces while traversing. We need to keep track of two indexes, one for the current character being red and the other for the current index in the output. Implementation: C++ // C++ program to evaluate a given expression #include <iostream> using namespace std; char *removeSpaces(char *str) { int i = 0, j = 0; while (str[i]) { if (str[i] != ' ') str[j++] = str[i]; i++; } str[j] = '\0'; return str; } // Driver program to test above function int main() { char str1[] = "gee k "; cout << removeSpaces(str1) << endl; char str2[] = " g e e k "; cout << removeSpaces(str2); return 0; } Outputgeek geek Time complexity: O(n) where n is the number of characters in the input string.Auxiliary Space: O(1) Approach 2: using in-built function The main idea in this approach is we will traverse the whole string and will delete the space by using the in-built erase function from C++ STL. Time complexity will be O(N) where N is the length of the string and the solution will also be the in-place solution. C++ //C++ program to remove spaces from //string using in-built function #include <bits/stdc++.h> using namespace std; //function to remove the spaces from string void removespace(string s) { //traversing the string for (int i = 0; i < s.length(); i++) { if (s[i] == ' ') { //using in-built function to erase element s.erase(s.begin() + i); i--; } } cout << s; } int main() { string s = "Gee k "; cout << "original string is: " <<s<<endl; cout << "final updated string: "; //function calling removespace(s); cout<<endl; string s1="G e e k"; cout << "original string is: " <<s1<<endl; cout << "final updated string: "; //function calling removespace(s1); return 0; } //this code is contributed by machhaliya muhammad Outputoriginal string is: Gee k final updated string: Geek original string is: G e e k final updated string: Geek Auxiliary Space: O(1), for erasing the character in the string it takes constant time. Comment More infoAdvertise with us Next Article C++ Program to remove spaces from a string K kartik Follow Improve Article Tags : Strings DSA Practice Tags : Strings Similar Reads Remove spaces from a given string Given a string, remove all spaces from the string and return it. Input: "g eeks for ge eeks "Output: "geeksforgeeks"Input: "abc d "Output: "abcd"Expected time complexity is O(n) and only one traversal of string. Below is a Simple Solution 1) Iterate through all characters of given string, do followi 8 min read Remove extra spaces from a string Given a string containing many consecutive spaces, trim all spaces so that all words should contain only a single space between them. The conversion should be done in-place and solution should handle trailing and leading spaces and also remove preceding spaces before common punctuation like full sto 14 min read Program for removing i-th character from a string Given a string S along with an integer i. Then your task is to remove ith character from S. Examples: Input: S = Hello World!, i = 7Output: Hello orld!Explanation: The Xth character is W and after removing it S becomes Hello orld! Input: S = GFG, i = 1Output: GGExplanation: It can be verified that a 5 min read Removing spaces from a string using Stringstream Solution to removing spaces from a string is already posted here. In this article another solution using stringstream is discussed. Algorithm: 1. Enter the whole string into stringstream. 2. Empty the string. 3. Extract word by word and concatenate to the string. Program 1: Using EOF. CPP // C++ pro 2 min read Program to Search a Character in a String Given a character ch and a string s, the task is to find the index of the first occurrence of the character in the string. If the character is not present in the string, return -1.Examples: Input: s = "geeksforgeeks", ch = 'k'Output: 3Explanation: The character 'k' is present at index 3 and 11 in "g 6 min read Like