Ad
  • Default User Avatar
  • Default User Avatar

    Can someome please explain me why cant I do the assignment like this : ans[i] = str[i]; ?
    Thanx

  • Custom User Avatar

    This solution adds extra spaces for new words which are allready uppercase, as far as I understood this was not the task. Also there is no allocation for on empty string which still needs termination (as far as I know strlen does not count the null termination).
    I think this produces a wrong output for "abc D" which is "abc D" and I think if called with "" is undefined behaivior or system dependend

  • Default User Avatar

    nah, I think it's to give extra space in case of memory overflow

  • Default User Avatar

    That really depends on the length of the strings you are going to be processing and your hardware, but now a days memory is cheap. The context of this problem is comments, there is usually a character limit, let's say you have a 1000 character limit, Then at best you'll be allocating 1kB for the comment and another 1kB for the answer, thats 2kB total. On the other hand if you count the number of non-vowel characters you are going to check the string twice no matter what and in the worst case scenario you are going to be allocating 2kB either way if the comment doesn't contain any vowel. It's a trade off, you are saving a little bit of memory for processing power.

  • Default User Avatar

    Yes, it's a trade off.
    In your solution, which does not waste memory, you check the whole string twice.
    Another solution would be to first create the wasteful buffer, write the solution into it, now determine the actual size.
    Create a new buffer, copy the string from the wasteful buffer, free the wasteful buffer, return the new memory optimized buffer.

    But hell, we are optimizing something really tiny and even if the string was quite large, then this third solution would be problematic, because temporarily you need almost twice the memory.

  • Custom User Avatar

    Did you forget to flag you reply as spoiler? as it appeared in my dashboard

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution