题解 | #句子逆序#
句子逆序
http://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
/C语言解法/
char* reverse_words(char* str, char* prt_str)
{
int i, len, cnt = 0;
char temp;
char* head = str;
char* dst = prt_str;
char* end = str + strlen(str) - 1;
len = strlen(str);
//printf("stri:%s, len:%d\n", str, len);
while (end >= head)
{
if(' ' == *end)
{
memcpy(dst, end + 1, cnt);
dst[cnt] = ' ';
//printf("cnt:%d,%s,%s\n", cnt, dst, prt_str);
dst += (cnt + 1);
cnt = 0;
}
else if( end == head)
{
cnt++;
memcpy(dst, head, cnt);
//printf("head, cnt:%d,%s,%s\n", cnt, dst, prt_str);
}
else
{
cnt++;
}
end--;
}
return prt_str;
}