本文介绍了高效的字符串反转代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好,我是一位经验丰富的专家C程序员,我写了这个代码来反转一个字符串。我想你们都可以从中学到一些东西! int reverse(char * reverseme){ int retval = - 1; if(retval!= NULL){ int len = strlen(retval){ if(len> 0){ int half = len>> 1; for(; retval< half; retval ++){ reverseme [retval] ^ = reverseme [ len-(retval + 1)]; reverseme [len-(retval + 1)] = reverseme [retval]; reverseme [retval] ^ = reverseme [len - (retval + 1)]; } } } 返回retval; } -Phil / CERisE 解决方案 不,你不是。 我对此表示怀疑。你应该重新考虑一下。它不是,因为你主题 声称高效。 显然,你是自负的,并且 b)错误 我/是/对不起,包括你和你的雇主。 我确定你是对的。毕竟,/ I /得知你比你声称的C程序员要少得多。 无用测试,因为retval被初始化(并且从未改变)到一个不等于NULL的值 第一次真正的失败。 retval是一个整数,并且 strlen()的参数是一个指向char的指针。使用字符串长度时,您无法从 获得有效结果。一个整数。 第二次失败:你没有#include声明 strlen()函数的标题。因此,您的编译器没有将上述 赋值标记为错误(不兼容类型的参数) 那么,字符串长度是多少?整数?什么时候它会大于零? $ blockquote class =post_quotes> { int half = len>> 1 ; 样式黑客:为了更清楚地知道半数代表什么类型的数字 ,你应该将其编码为 int half = len / 2; 当然,由于len是无意义的值,因此一半也将是 无意义的值。 第一次迭代时未定义的行为,当retval == -1 reverseme [-1]超出范围时。 第一次迭代时未定义的行为 - 同样的原因 第一次迭代时未定义的行为 - 同样的原因 另外,你搞砸了xor技巧 ,和/ / / $ / $ 字符串末尾的值无效。 头端现在包含一个0x00的数组(最多 错误计算的中点),而尾端包含头端xor尾部的 结果。 此返回值的用途是什么? - Lew Pitcher Master Codewright& amp ; JOAT-in-training |已注册的Linux用户#112576 http://pitcher.digitalfreehold.ca/ |可根据要求提供GPG公钥 ---------- Slackware - 因为我知道我在做什么。 ------ s / C程序员/ PLONKed巨魔/ - Er ********* @ sun.com Hello, I''m a highly experienced expert C programmer and I''ve writtenthis code to reverse a string in place. I think you could all learnsomething from it! int reverse(char* reverseme){int retval=-1;if(retval!=NULL){int len=strlen(retval){if(len>0){int half=len>>1;for(;retval<half;retval++){reverseme[retval]^=reverseme[len-(retval+1)];reverseme[len-(retval+1)]=reverseme[retval];reverseme[retval]^=reverseme[len-(retval+1)];}}}return retval;} -Phil/CERisE 解决方案 No, you are not. I doubt it. You should rethink it. It is not, as you subject lineclaims "highly efficient".Apparently, you area) egotistical, andb) wrong I am /so/ sorry, both for you and your employer. I''m sure that you are right. After all, /I/ learned that you are much lessthan the C programmer that you claim to be. Useless test, as retval was initialized (and never altered) to a valuethat will not equate to NULL First real failure. retval is an integer, and the argument tostrlen() is a pointer-to-char. You can''t get a valid result fromtaking the "string length" of an integer. Second failure: you did not #include the header that declares thestrlen() function. Thus, your compiler did not flag the aboveassignment as an error (argument of incompatable type) So, what is the "string length" of an integer? And when would it begreater than zero? Style hack: To be clearer to the intent of what sort of numberhalf represents, you should have coded this asint half = len / 2;Of course, since len is a nonsense value, half will also be anonsense value. Undefined behaviour on first iteration, when retval == -1reverseme[-1] is out of bounds. Undefined behaviour on first iteration - same reason Undefined behaviour on first iteration - same reason Plus, you''ve screwed up the "xor trick", and wiped out /both/ends of the string with invalid values. The head end now contains an array of 0x00 (up tothe ill-computed "midpoint"), while the tail end contains theresults of the head end xor the tail end. Of what use is this return value? --Lew Pitcher Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request---------- Slackware - Because I know what I''m doing. ------s/C programmer/PLONKed troll/ -- Er*********@sun.com 这篇关于高效的字符串反转代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 07:41