- 论坛徽章:
- 0
|
given that char *str, which is either NULL or valid string with '\0' teminated, the following function is one implementation of reversing of str by O(1) in space and O(n) in time and not use any <string.h> functions. The code has flaws to crash, please correct them by keeping major code.
void reverse(char *str) {
char *ptr = str;
while(ptr && *ptr != '\0') {
ptr++;
}
if (ptr)
ptr--;
while(str && str < ptr) {
char c = *str;
*str = *ptr;
*ptr = c;
str++;
ptr--;
}
}
|
Who can help me to correct them? Thanks.
[ 本帖最后由 grizzly 于 2007-10-3 22:01 编辑 ] |
|