Day 76/100 100 Days of Code

Day 76/100 100 Days of Code


I didn't have much time so I decided to solve some problems on codewars. I spent the session on a problem that was very exciting.

I had to create a function that removes the first and last characters. What made this interesting was the space character at the end of the output. I believe the space was there because the terminating character was being copied when using strcpy. I decided to create a temporary string and copy each character-starting from the second character- using strncpy. Then, I used strcpy to copy the entire string to the dst string variable.

#include <stdio.h>
#include <string.h>
#include <malloc.h>

char* remove_char(char* dst, const char* src)
  /* src is the input string */
  /* your solution should write the result into dst and return it */
  size_t getStringLength = strlen(src);
  char *temp = calloc(getStringLength - 2, sizeof(char));

  for (size_t i = 0; i < getStringLength - 2; i++)
    char getCharacter = src[i + 1];

    strncpy(&temp[i], &getCharacter, 1);

  strcpy(dst, temp);

  return dst;