|
My C++ solution for the first questoin.
No library function is used, except strlen()
I think it is easy for us to implement strlen(), so in order to make the code concise, i use strlen, instead write it myself.
If you have any good suggestion about my code and found any falut,please let me know.
Very appreciated!
void revertString(char *str)
{
if(!str) return;
int len = strlen(str);
if( len < 1) return ;
int low =0;
int high = len -1;
while(low < high)
{
char tmp = str[low];
str[low] = str[high];
str[high] = tmp;
low ++;
high --;
}
int step =0;
while( step < len)
{
while(str[step] == ' ' && step < len )
{
step ++;
}
low = step;
while(str[step] != ' ' && step < len)
{
step ++;
}
high = step -1;
while( low < high)
{
char tmp = str[low];
str[low] = str[high];
str[high] = tmp;
low ++;
high --;
}
}
} |
|