Move spaces to front of string in single traversal | CPP program to bring all spaces in front of string using swapping technique ; Function to find spaces and move to beginning ; Keep copying non - space characters ; Move spaces to be beginning ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void moveSpaceInFront ( char str [ ] ) { int i = strlen ( str ) ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = ' ▁ ' ; } int main ( ) { char str [ ] = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " ; moveSpaceInFront ( str ) ; cout << str ; return 0 ; }