top of page

C++ Program to remove spaces from a given string

We are given a string in which we have to remove all the spaces and return the string.


Example:

Input: "p rog rammer s doo r"
Output: "programmersdoor"
 

Implementation:

#include <iostream>
using namespace std;

void remove_Space(char *str)
{
    int count = 0;
    
    for(int i = 0; str[i]; i++)
    {
        if(str[i] != ' ')
            str[count++] = str[i];
    }
    str[count] = '\0';
}

int main()
{
    char str[] = "p rog rammer s doo r";
    remove_Space(str);
    cout<<str;
    return 0;
}
Output:
programmersdoor
 

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

Follow Programmers Door for more.

Comments


bottom of page