不使用库函数,仅用指针原地反转一个字符串
函数原型:
void reverse_string(char * str)基本思想:设置两个指针:begin和end
1:将begin指向字符串开始,end指向字符串结尾
2:交换begin和end所指向的值
3:begin后移,end前移
重复以上步骤直到begin和end指向同一个位置
源码:
void reverse_string(char * str)
{
char * begin = str;
char * end = str;
while(*end != '\0')
{
end++;
}
end--;
char temp;
while(begin != end)
{
temp = *begin;
*begin = *end;
*end = temp;
begin++;
end--;
}
printf(str);
}
399

被折叠的 条评论
为什么被折叠?



