-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrfun.c
More file actions
110 lines (97 loc) · 1.74 KB
/
strfun.c
File metadata and controls
110 lines (97 loc) · 1.74 KB
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
#include "shell.h"
/**
* _strlen - lenght string
* @str: string
* Return: len
*/
unsigned int _strlen(char *str)
{
unsigned int len = 0;
for (len = 0; str[len]; len++)
;
return (len);
}
/**
* _puts - print function
* @str: string
* Return: len
*/
ssize_t _puts(char *str)
{
ssize_t num = 0, len = 0;
num = _strlen(str);
len = write(STDOUT_FILENO, str, num);
if (len != num)
{
perror("Fatal Error");
return (-1);
}
return (len);
}
/**
* _strcat - function concatenates
* @strc1: string 1
* @strc2: string 2
* Return: Newstring
*/
char *_strcat(char *strc1, char *strc2)
{
char *NewString;
unsigned int len1 = 0, len2 = 0, Newlen;
unsigned int iter1, iter2;
if (strc1 == NULL)
len1 = 0;
else
for (len1 = 0; strc1[len1]; len1++)
;
if (strc2 == NULL)
len2 = 0;
else
for (len2 = 0; strc2[len2]; len2++)
;
Newlen = len1 + len2 + 2;
NewString = malloc(Newlen * sizeof(char));
if (NewString == NULL)
return (NULL);
for (iter1 = 0; iter1 < len1; iter1++)
NewString[iter1] = strc1[iter1];
NewString[iter1] = '/';
for (iter2 = 0; iter2 < len2; iter2++)
NewString[iter1 + 1 + iter2] = strc2[iter2];
NewString[len1 + len2 + 1] = '\0';
return (NewString);
}
/**
* _strcmp - compares the string
* @s1: string
* @s2: string
* Return: 0
*/
int _strcmp(char *s1, char *s2)
{
int i;
for (i = 0; s1[i] == s2[i]; i++)
{
if (s1[i] == '\0')
return (0);
}
return (s1[i] - s2[i]);
}
/**
* _strdup - duplicate string
* @strtodup: string
* Return: copy
*/
char *_strdup(const char *strtodup)
{
char *copy;
int len, i;
if (strtodup == 0)
return (NULL);
for (len = 0; strtodup[len]; len++)
;
copy = malloc((len + 1) * sizeof(char));
for (i = 0; i <= len; i++)
copy[i] = strtodup[i];
return (copy);
}