Bonkers

Ash Berlin ash_cpan at firemirror.com
Thu May 10 23:22:46 BST 2007


Nicholas Clark wrote:
> On Thu, May 10, 2007 at 10:50:54PM +0100, Stig Brautaset wrote:
> 
>> No, I was not intending the xor-trick. I don't like those types of  
>> questions. Rather, I'd try to see if they understood pointers enough  
> 
> I think I'd argue that it's a wrong answer.
> Unless it's an assembler test.
> 
> I read online that in tech screening interviews Google ask C programmers
> questions relating to those types of bit operation tricks, and I'm really
> not convinced that any sane C programmer should be using them.
> 
>> to get rid of the index variables. Something like the following:
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main(void)
>> {
>>     char s[] = "hello world";
>>
>>     char *h = s;
>>     char *t = strchr(s, '\0');
> 
> er, why not strlen() and then a pointer addition?
> That's not the first time I've seen that idiom, but I can't see how an
> implementation of strchr() is going to be tighter than one of strlen()
> 
> Nicholas Clark

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;

   }
}

And yes, i did miss of the hashes, since it was my first time driving a 
mac and i couldn't find the exact combo to get a # (I knew it was on the 
3 key somewhere, I just didn't care enough to actualy test the program ;))


More information about the london.pm mailing list