Skip to content

Commit e9fb0e8

Browse files
committed
Add 2
1 parent 84a8e3c commit e9fb0e8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

pointers_arrays_strings/2-strncpy.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "main.h"
2+
3+
/**
4+
* _strncpy - Copies at most an inputted number
5+
* of bytes from string src into dest.
6+
* @dest: The buffer storing the string copy.
7+
* @src: The source string.
8+
* @n: The maximum number of bytes to copied from src.
9+
*
10+
* Return: A pointer to the resulting string dest.
11+
*/
12+
char *_strncpy(char *dest, char *src, int n)
13+
{
14+
int i = 0, src_length = 0;
15+
16+
while (src[i++])
17+
src_length++;
18+
19+
for (i = 0; src[i] && i < n; i++)
20+
dest[i] = src[i];
21+
22+
for (i = src_length; i < n; i++)
23+
dest[i] = '\0';
24+
25+
return (dest);
26+
}

pointers_arrays_strings/2-strncpy.c~

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* File: 2-strncpy.c
3+
* Auth: Brennan D Baraban
4+
*/
5+
6+
#include "holberton.h"
7+
8+
/**
9+
* _strncpy - Copies at most an inputted number
10+
* of bytes from string src into dest.
11+
* @dest: The buffer storing the string copy.
12+
* @src: The source string.
13+
* @n: The maximum number of bytes to copied from src.
14+
*
15+
* Return: A pointer to the resulting string dest.
16+
*/
17+
char *_strncpy(char *dest, char *src, int n)
18+
{
19+
int index = 0, src_len = 0;
20+
21+
while (src[index++])
22+
src_len++;
23+
24+
for (index = 0; src[index] && index < n; index++)
25+
dest[index] = src[index];
26+
27+
for (index = src_len; index < n; index++)
28+
dest[index] = '\0';
29+
30+
return (dest);
31+
}

0 commit comments

Comments
 (0)