标签:
problem:
Implement a function void reverse(char *str) in C and C++ which reverse a null-terminated string.
The solution:
1. use another pointer end point to str.
2. move *end to end of str and move to forward pass null.
3. exchage str++ and end--, when str<end.
4. there two pointer, one move from end to begin, another is move from begin to end.
the code:
void reverse(char *str){
char* end = str;
char tmp;
if(str){
while(*end){
end++;
}
end--;
while(str<end){
tmp =*str;
++str = *end;
--end = tmp;
}
}
}
标签:
原文地址:http://www.cnblogs.com/whaochen/p/4731627.html