Skip to content

Commit

Permalink
adding 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfosse committed Mar 10, 2017
1 parent 278ec8f commit 71890b3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 0x0B-more_malloc_free/1-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "holberton.h"
#include <stdio.h>
#include <stdlib.h>

/**
* 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);
}
54 changes: 54 additions & 0 deletions 0x0B-more_malloc_free/1-string_nconcat.c
Original file line number Diff line number Diff line change
@@ -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);
}
Binary file added 0x0B-more_malloc_free/b
Binary file not shown.
1 change: 1 addition & 0 deletions 0x0B-more_malloc_free/holberton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 71890b3

Please sign in to comment.