-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrfunc.c
123 lines (107 loc) · 2.06 KB
/
strfunc.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
113
114
115
116
117
118
119
120
121
122
123
#include "shell.h"
/**
* _puts - writes a string to standard output
* @str: string to write
*
* Return: number of chars printed or -1 on failure
*/
ssize_t _puts(char *str)
{
ssize_t num, len;
num = _strlen(str);
len = write(STDOUT_FILENO, str, num);
if (len != num)
{
perror("Fatal Error");
return (-1);
}
return (len);
}
/**
* _strdup - returns pointer to new mem alloc space which contains copy
* @strtodup: string to be duplicated
*
* Return: a pointer to the new duplicated string
*/
char *_strdup(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);
}
/**
* _strcmpr - compares two strings
* @strcmp1: first string, of two, to be compared in length
* @strcmp2: second string, of two, to be compared
* Return: 0 on success, anything else is a failure
*/
int _strcmpr(char *strcmp1, char *strcmp2)
{
int i;
i = 0;
while (strcmp1[i] == strcmp2[i])
{
if (strcmp1[i] == '\0')
return (0);
i++;
}
return (strcmp1[i] - strcmp2[i]);
}
/**
* _strcat - concatenates two strings
* @strc1: first string
* @strc2: second string
* Return: pointer
*/
char *_strcat(char *strc1, char *strc2)
{
char *newstring;
unsigned int len1, len2, newlen, i, j;
len1 = 0;
len2 = 0;
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 (i = 0; i < len1; i++)
newstring[i] = strc1[i];
newstring[i] = '/';
for (j = 0; j < len2; j++)
newstring[i + 1 + j] = strc2[j];
newstring[len1 + len2 + 1] = '\0';
return (newstring);
}
/**
* _strlen - returns the length of a string
* @str: string to be measured
* Return: length of string
*/
unsigned int _strlen(char *str)
{
unsigned int len;
len = 0;
for (len = 0; str[len]; len++)
;
return (len);
}