Ad
  • Default User Avatar

    I spit my coffee laughing, but then I thought about it some more. On most modern operating systems no memory is actually allocated until it is written or read. The program is not allocating gigabytes of memory, it's using gigabytes of address space and only allocating the pages that are actually touched. I honestly can't tell if this was a joke that was unintentionally clever.

  • Default User Avatar

    Read the requirements very carefully, you're missing a case.

  • Default User Avatar

    I have a feeling this is not what was intended.

  • Default User Avatar

    The malloc function returns uninitialized memory. If this happens to have a 0 in the first character then it will work, if it doesn't then it won't work. So the same code may work on your computer, but on Codewars will not. You should be using calloc instead which writes 0's to the memory it returns. Unless you're allocating extremely large regions of memory there's no practical reason to use malloc anymore.

  • Default User Avatar

    sizeof(src) doesn't do what you think it does, it's returning the size of the pointer type. You want strlen(src) instead.

    Also, malloc isn't required to return initialized memory. It might have junk data in it, if there is junk data then strcat will do strange things and possibly segfault. You should use calloc instead, which returns 0-initialized memory.

  • Default User Avatar

    You shouldn't compare strings with ==. This will compare the address of the strings and will usually work, but depending on how the program was linked it's conceivable that there could be multiple identical string literals with different addresses. And of course it will totally break if the strings come from user input.

  • Default User Avatar

    You should never printf(string). If string happens to contain format specifiers, who knows what will happen? You can printf("%s", string), or puts(string), or any number of other ways.

  • Default User Avatar

    I like this. I did more or less the same thing, but used strcmp and looped through. The sparse lookup table is definitely faster, but you might want to make it static so it's not copied to the stack every time the function is run.

  • Default User Avatar

    Your break statements are unnecessary, return already breaks.

  • Default User Avatar

    Your c1, c2, etc variables here are actually unnecessary, you can just use the string literals in the if statement.