Bonkers

Nicholas Clark nick at ccl4.org
Thu May 10 23:46:44 BST 2007


On Thu, May 10, 2007 at 11:22:46PM +0100, Ash Berlin wrote:

> ISTR my solution to that exact question was:
> 
> include <stdio.h>
> include <stdlib.h>
> 
> char* reverse(char* str) {
>   int i, len = strlen(str), len_2 = len/2;
> 
>   for (i=0; i < len_2; i++) {
>     char s = str[i];
>     str[i] = str[len-1-i];
>     str[len-1-i] = s;
> 
>   }
> }

I think that yours breaks on an empty string.
[That was something I realised was an edge case after thinking about the
problem for about 5 minutes]
Whereas Stig's doesn't, IIRC

Actually, I'm not convinced that yours is correct for strings of odd lengths
either.

[but mine fails on all these points, as I didn't offer a solution]

Nicholas Clark


More information about the london.pm mailing list