Created
May 6, 2019 20:44
-
-
Save boki1/7e55024cbff7f9602e3f7ee317811073 to your computer and use it in GitHub Desktop.
Implementation of c stdlib functions strlen, strcpy & strcpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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