如何彻底扭转一个C风格的字符串 [英] How to reverse a C style string completely
问题描述
我想扭转一个C风格的字符串(空值终止)完全,即包括空字符。
I'd like to reverse a C style string (null-terminated) completely, i.e. including the null character.
空字符应当是在翻转串的第一个元素
The null character should be the first element in the reversed string.
推荐答案
这实际上是pretty的方便:
That's actually pretty easy:
char *reversed = "";
:)
C字符串开头的'\ 0'
是一个空字符串。除非你做的不是字符串的东西(如添加+1),与预期的内存(指针+大小)的指针或使用的功能也不要紧什么来的'\ 0'$的背后C $ C>
A C string starting with a '\0'
is an empty string. And unless you do things (like adding +1) with the pointer or use functions which expect memory (pointer+size) instead of strings it doesn't matter what comes behind the '\0'
如果您真的需要真正的字符串,经过present在内存中的'\ 0'
:以这写的逆转字符串给定的记忆功能位置(无效strreverse(字符* DEST,为const char *字符串)
)。
然后调用它像这样:字符DEST [ENOUGH_SPACE] DEST [0] ='\ 0'; strreverse(DEST + 1,YOUR_STRING);
If you actually needed the real string to be present in memory after the '\0'
: Take the function which writes your reversed string to a given memory location (void strreverse(char *dest, const char *string)
).
Then call it like that: char dest[ENOUGH_SPACE]; dest[0] = '\0'; strreverse(dest + 1, YOUR_STRING);
这篇关于如何彻底扭转一个C风格的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!