Problem
Is this even possible? Move the spaces to the starting of the string in a c style string. In place within one iteration.
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Move the spaces to the starting of the string in single iteration
Created Date : 15-06-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
using namespace std;
void PrintString(char *str)
{
while(*str != '\0'){
cout << setw(3) << *str++ ;
}
cout << endl;
}
inline void SwapChar(char& a, char& b)
{
char t = a;
a = b;
b = t;
}
void MoveSpaceToStart(char* str)
{
if(str == NULL) return;
char *p(str);
int index(-1);
char t;
while(p[0] != '\0'){
if(p[0] == ' ' || p[0] == '\t'){
SwapChar(p[0], str[++index]);
}
p++;
}
}
int main(int argc, char* argv[])
{
char testCases[][20] = {
"Hello world",
" Hello ",
" Hello",
"A B\tC D E ",
""
};
for(int i = 0; i < 20; ++i){
cout << setw(3) << left << i;
}
cout << endl;
for(int i = 0; i < sizeof(testCases)/ sizeof(testCases[0]); ++i){
cout << "Before processing" << endl;
PrintString(testCases[i]);
MoveSpaceToStart(testCases[i]);
cout << "After processing" << endl;
PrintString(testCases[i]);
cout << "--------------------" << endl;
}
return 0;
}
Output
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Before processing
H e l l o w o r l d
After processing
e l l o H w o r l d
--------------------
Before processing
H e l l o
After processing
o H e l l
--------------------
Before processing
H e l l o
After processing
H e l l o
--------------------
Before processing
A B C D E
After processing
D A E C B
--------------------
Before processing
After processing
--------------------
Press any key to continue . . .