简体   繁体   English

我错过了我的功能吗?

[英]Am I missing something in my function?

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];
    }
}
  • When i is 0 you'll be accessing string[len] which is incorrect as the valid index in an array of length len are [0,len-1] i0您将访问string[len] ,这是不正确的,因为长度为len的数组中的有效索引为[0,len-1]

If I understand you intent correctly you are trying to reverse the string but I can see a few things missing: 如果我理解你的意图正确你试图扭转字符串但我可以看到一些缺失的东西:

  • You are not swapping. 你没有交换。
  • Also the swapping should happen for one half of the array, not for the entire array. 此外,交换应该发生在阵列的一半,而不是整个阵列。

The following snippet fixes these issues: 以下代码段修复了这些问题:

int len = strlen(string);
for(int i = 0; i < len/2; i++) {
    swap(string[len-i-1],string[i]);
}

First of all, you would get an error on line 6. 首先,你会在第6行得到一个错误。

Change the { into } . 改变{ 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. 这不是函数名称声明的内容。

This is tagged C++, do it the C++ way... 这是标记的C ++,用C ++方式做...

std::string ReverseString(std::string str) 
{
  std::reverse(str.begin(), str.end());
  return str;
}

should be string[i] = string[len-i-1]; 应该是string[i] = string[len-i-1];

// added (untested):  

void ReverseString(char * string) {   
    int len = strlen(string);  
    for(int i = 0; i < len / 2; i++)  
    {  
         string[i] ^= string[len-i-1];  
         string[len-i-1] ^= string[i];  
         string[i] ^= string[len-i-1];  
    }  
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我错过了什么吗? - Am I missing something? 这是一个错字还是我错过了什么? - is this a typo or am i missing something? 这是文档中的小错误,还是我缺少什么? - Is this a minor error in the documentation or am I missing something? vector :: empty()错误,还是我错过了什么? - vector::empty() bugged, or am I missing something? 我在 cin.get() 中遗漏了什么吗? - Am I missing something with cin.get()? 我的iOS金属计算内核是否存在编译器错误,或者我缺少某些东西? - Is there a compiler bug for my iOS metal compute kernel or am I missing something? 同样的逻辑适用于c ++但不能在python中最大化堆栈,我的代码中缺少一些东西 - Same logic is working for c++ but not in python for maximum in stack,is there something that i am missing in my code 为什么我的 class 函数在 main 中是“未定义的”我在代码中遗漏了什么? - Why are my class functions being “undefined” in main am i missing something in the code? 是否可以通过其地址本地访问另一个线程\\此优化有效\\我遗漏了一些大东西吗? - Can another thread access function local by its address \ is this optimization valid \ am I missing something big? 我认为下面的陈述不正确或者我错过了什么? - I think the statement below is incorrect or am I missing something?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM