Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C++ Program to remove spaces from a string

  • Difficulty Level : Easy
  • Last Updated : 17 May, 2018

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 current character being red and other for current index in output.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

// 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];
    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;



Time complexity of above implementation is O(n) where n is number of characters in input string.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!