diff --git a/0x0B-more_malloc_free/1-main.c b/0x0B-more_malloc_free/1-main.c new file mode 100644 index 0000000..18d499e --- /dev/null +++ b/0x0B-more_malloc_free/1-main.c @@ -0,0 +1,18 @@ +#include "holberton.h" +#include +#include + +/** + * main - check the code for Holberton School students. + * + * Return: Always 0. + */ +int main(void) +{ + char *concat; + + concat = string_nconcat("Holberton ", "School !!!", 6); + printf("%s\n", concat); + free(concat); + return (0); +} diff --git a/0x0B-more_malloc_free/1-string_nconcat.c b/0x0B-more_malloc_free/1-string_nconcat.c new file mode 100644 index 0000000..f578e0a --- /dev/null +++ b/0x0B-more_malloc_free/1-string_nconcat.c @@ -0,0 +1,54 @@ +#include "holberton.h" + +/** + * _strlen - measures the length of a string. + * Descritpion: measures the string s. + * @s: string to be messured/ + * Return: returns length of s. + */ +unsigned int _strlen(char *s) +{ + unsigned int count; + + count = 0; + while (s[count] != '\0') + { + count++; + } + return (count); +} + +/** + * string_nconcat - cats two strings by amount n. + * @s1: string 1. + * @s2: string 2. + * @n: amount to take. + * Return: returns pointer to new string, NULL on fail. + */ +char *string_nconcat(char *s1, char *s2, unsigned int n) +{ + char *temp; + unsigned int h, i, size; + + if (s1 == NULL) + s1 = ""; + if (s2 == NULL) + s2 = ""; + + if (n > _strlen(s2)) + size = _strlen(s1) + _strlen(s2); + else + size = _strlen(s1) + n; + + temp = malloc((size + 1) * sizeof(char)); + if (temp == NULL) + return (NULL); + + for (h = 0; s1[h] != '\0'; h++) + temp[h] = s1[h]; + for (i = 0; h < size && s2[i] != '\0'; i++, h++) + temp[h] = s2[i]; + + temp[h] = '\0'; + return (temp); +} diff --git a/0x0B-more_malloc_free/b b/0x0B-more_malloc_free/b new file mode 100755 index 0000000..54c335d Binary files /dev/null and b/0x0B-more_malloc_free/b differ diff --git a/0x0B-more_malloc_free/holberton.h b/0x0B-more_malloc_free/holberton.h index 0b0248c..ee82061 100644 --- a/0x0B-more_malloc_free/holberton.h +++ b/0x0B-more_malloc_free/holberton.h @@ -6,5 +6,6 @@ void *malloc_checked(unsigned int b); int _putchar(char c); +char *string_nconcat(char *s1, char *s2, unsigned int n); #endif