Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

  for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' ')) 
    *p = '_'; 
OW MY BRAIN.


That's fairly redundant.

    char *p;
    while (p = strchr(tmpfn, ' '))
        *p = '_';


I love that the O(n^2 / 2) algorithm for this problem is modded 4x the O(n) one.


If you meant to be O(n), you should have used p=strchr(p,' ') for the next-iteration part of your for loop.


I didn't write that code, Google did. I wonder if the person who wrote it can figure out how many golf balls fit in a school bus?


OW MY BRAIN.

  char *cp = NULL;
  for(cp = tmpfn; cp && *cp; cp++)
    if(isspace(*cp))
      *cp = '_';


char cp = tmpfn; for(; cp; cp++) if(cp == ' ') cp = '_';

my brain too


You're replacing all spaces in a string with underscores, until you reach the end of a string or there are no spaces left...

Cool. But what does this have to do with the article? (I apologize if I'm missing something, it is 3am afterall)


What's the purpose of the "&& *p"? Would strchr ever return a pointer to a null terminator?


Yes if you pass it a pointer to a null string


NULL is how strchr tells you the character you're looking for isn't present. (Yes, the *p check is also superfluous).

Kids these days.


It was in the presentation.


What's wrong with that code, Computer Guru? =)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: