Skip to content

Instantly share code, notes, and snippets.

@boki1
Created May 6, 2019 20:44
Show Gist options
  • Save boki1/7e55024cbff7f9602e3f7ee317811073 to your computer and use it in GitHub Desktop.
Save boki1/7e55024cbff7f9602e3f7ee317811073 to your computer and use it in GitHub Desktop.
Implementation of c stdlib functions strlen, strcpy & strcpy
#include <stdio.h>
#include <stdlib.h>
size_t _strlen(const char *s)
{
size_t size = 0;
while (*s++)
++size;
return size;
}
void _strcat(char *to, const char *from) {
while (*to)
to++;
while (*to++ = *from++);
}
void _strcpy(char *dest, const char *src) {
while (*dest++ = *src++);
}
int main() {
char test_string[11] = "hello ";
char test_concat[] = "world";
_strcat(test_string, test_concat);
int len = _strlen(test_string);
char *buffer = malloc(len + sizeof "");
_strcpy(buffer, test_string);
printf("%s is %d chars long.\n", buffer, len);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment