我的函数中是否缺少某些内容?
这就是我到目前为止所拥有的,但我不断收到错误。有什么帮助吗?
void ReverseString(char* string) {
int len = strlen(string);
for(int i = 0; i < len; i++)
{
string[i] = string[len-i];
}
}
This is what I have so far and I keep getting an error. Any help?
void ReverseString(char* string) {
int len = strlen(string);
for(int i = 0; i < len; i++)
{
string[i] = string[len-i];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
i
为0
时,您将访问string[len]
这是不正确的长度数组中的有效索引
len
是[0,len-1]
如果我正确理解您的意图,您正在尝试反转字符串,但我可以看到缺少一些东西:
数组的一半,不适合
整个阵列。
以下代码片段修复了这些问题:
i
is0
you'll be accessingstring[len]
which is incorrect asthe valid index in an array of length
len
are[0,len-1]
If I understand you intent correctly you are trying to reverse the string but I can see a few things missing:
one half of the array, not for the
entire array.
The following snippet fixes these issues:
首先,您会在第 6 行收到错误。
将
{
更改为}
。然后再试一次。First of all, you would get an error on line 6.
Change the
{
into}
. Then try again.除了两个已经提到的错误之外:
您将从原始字符串中生成回文。上半场将等于下半场逆转。不过,下半场将保持不变。这不是函数名称所声明的内容。
Besides two already mentioned errors:
You'll make a palindrom out of the original string. The first half will became equal to second half inversed. However, the second half will remain the same. This is not what the function name declares.
这是标记为 C++ 的,用 C++ 的方式来做......
This is tagged C++, do it the C++ way...
应该是 string[i] = string[len-i-1];
should be
string[i] = string[len-i-1];