Skip to main content

Command Palette

Search for a command to run...

Day 76/100 100 Days of Code

Codewars

Updated
1 min readView as Markdown
Day 76/100 100 Days of Code
C

AKA Chris, is a software developer from Athens, Greece. He started programming with basic when he was very young. He lost interest in programming during school years but after an unsuccessful career in audio, he decided focus on what he really loves which is technology.

He loves working with older languages like C and wants to start programming electronics and microcontrollers because he wants to get into embedded systems programming.

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;
}

100 Days of Code

Part 1 of 50

100 days of code is a good initiative to go into hard mode and spend more time in programming. These 100 days will be focused on completing projects and research.