Remove spaces from a given string | An efficient C ++ program to remove all spaces from a string ; Function to remove all spaces from a given string ; To keep track of non - space character count ; Traverse the given string . If current character is not space , then place it at index ' count + + ' ; str [ count ++ ] = str [ i ] ; here count is incremented ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void removeSpaces ( char * str ) { int count = 0 ; for ( int i = 0 ; str [ i ] ; i ++ ) if ( str [ i ] != ' ▁ ' ) str [ count ] = ' \0' ; } int main ( ) { char str [ ] = " g ▁ eeks ▁ for ▁ ge ▁ eeks ▁ " ; removeSpaces ( str ) ; cout << str ; return 0 ; }