• char* function(const char *str, char remove, unsigned int length) { char *newstr = new char[length+1]; memset(newstr, 0, length + 1); unsigned int innerIndex = 0; for (int i = 0; i < length; i++) { if (str[i] != remove) { newstr[innerIndex] = str[i]; innerIndex++; } } newstr[length] = '\0'; return newstr; }
    • mushroom
      The question only stated there were two arguments. I would remove the length declaration from the arguments and add: unsigned int length = strlen(str);
    • brandon1234
      You assume the string is properly null terminated. If it is not the function will access memory outside the proper bounds that it should which could cause unexpected Behavior. I have seen Behavior like this cause A Fault in the program causing it to crash.
    • brandon1234
      If you read your C api you would know that string length depends on the null terminating character. That assignment was designed very poorly and very pathetically and this is coming from a professional programmer

Copyright 2020, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy