Ad
  • Custom User Avatar

    Lol I'm the same way. I enjoy seeing these one line solutions, though, because it helps me learn how to write more efficient code

  • Default User Avatar
    • you do not allocate enough memory in all cases : "zz" -> "26 26" which needs 6 bytes, you do allocate 6, but since your solutions has a trailing space which is then erased, you need 7 bytes. Currently you are writing out-of-bounds in this case
    • you return a string literal ("") when there are no letters in the input. the tests expect a heap-allocated string, so they will crash when they try to free() the string literal
    • you use strcat() and strncat() on unitialized memory. these functions look for a null byte to start writing; the memory returned by malloc() is NOT 0-initialized; neither are local (stack) variables like str_num, unless you explicitely initialize them with char str_num[4] = {0}. str(n)cat() will keep scanning the memory until it finds a 0 byte, so your program is unpredictable
  • Default User Avatar

    imo well it depends on the situation actually but unfortunately i think it is difficult to actually return a literal with a zero in front since some languages like python prohibit base 10 integers with leading zeros
    ofc its nice to remember that for example an int literal is 32 bits long in java and that there are leading zeros there but i think for the sake of this exercise it is better to have the ints without leading ints