Case1: 从hello world 翻转成 world hello
/*大概的理念是先把所有字符串都翻转了,所以hello world 编程dlrow olleh. 然后再每个字符之间翻转,变成world hello(以空位为分界点)*/
Case2: 从hello world翻转成olleh dlrow
/*大概的理念是以空格为分界点,每个字符串之间翻转*/
void reverseWords(char str[])
{
if (!str) return;
int cur=0, start =0;
while(str[cur])
{
if(str[cur]==SPECIAL_CHAR||str[cur]== " ")
{
cur=cur-1;
reserver(str, start, cur);
start=cur+1;
}
cur++;
}
}
void reverse(char *str, int beg, int end)
{
while(beg<end)
{
char temp=str[beg];
str[beg]=str[end];
str[end]=temp;
beg++;
end--;
}
}
考察三个方面,一是对指针和字符串的理解,二是是否进行合法性检查,例如输入参数为空指针时是否进行检查,三是返回值是否是恰当
char *revstr(char *str, size_t len)
{
char *start = str;
char *end = str + len - 1;
char ch;
if (str != NULL)
{
while (start < end)
{
ch = *start;
*start++ = *end;
*end-- = ch;
}
}
return str;
}