-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux_functions_1.c
112 lines (97 loc) · 1.5 KB
/
aux_functions_1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "main.h"
/**
* string_printer - prints strings
* @s: string to be printed
*
* Return: number of chars printed in string
*/
int string_printer(char *s)
{
int i, n = 0;
if (s != NULL)
{
for (i = 0; s[i] != '\0'; i++)
{
_putchar(s[i]);
}
return (i);
}
else
{
_putchar('(');
_putchar('n');
_putchar('u');
_putchar('l');
_putchar('l');
_putchar(')');
n = 6;
return (n);
}
}
/**
* _putchar - writes a character to stdout
* @c: char to print
*
* Return: 1 (success), -1 (error)
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* _strdup - duplicates a string
* @str: string to be duplicated
*
* Return: pointer to new string (success), NULL if string or malloc is NULL
*/
char *_strdup(char *str)
{
int i = 0, length = 0;
char *newstr;
if (str == NULL)
return (NULL);
while (str[i] != '\0')
{
length++;
i++;
}
newstr = malloc(sizeof(char) * (length + 1));
for (i = 0; i < str[i]; i++)
newstr[i] = str[i];
newstr[i] = '\0';
return (newstr);
}
/**
* _strlen - returns length of string
* @s: string to be measured
*
* Return: number of chars, except '\0'
*/
int _strlen(char *s)
{
int i = 0, length = 0;
while (s[i] != '\0')
{
length++;
i++;
}
return (length);
}
/**
* _strcpy - copies a string
* @src: string to be copied
* @dest: pointer to destination string
*
* Return: value of dest
*/
char *_strcpy(char *dest, char *src)
{
int i = 0;
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}