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