forked from BD20171998/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.c
108 lines (82 loc) · 1.57 KB
/
strings.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
#include "holberton.h"
/**
* _strlen - gives length of a string
* @s: string
* Return: returns length of string
*/
int _strlen(char *s)
{
int l = 0;
while (s[l] != '\0')
l++;
return (l);
}
/**
* _strcmp - compares two strings
* @s1: string 1
* @s2: string 2
* Return: comparison of two strings
*/
int _strcmp(char *s1, char *s2)
{
while (*s1)
{
if (*s1 != *s2)
break;
s1++;
s2++;
}
return (*s1 - *s2);
}
/**
* _strcpy - copies string to another string
* @dest: string 1
* @src: string 2
* Return: string copy
*/
char *_strcpy(char *dest, char *src)
{
int a;
for (a = 0; a <= _strlen(src); a++)
dest[a] = src[a];
return (dest);
}
/**
* _strdup - function that returns a pointer to a newly allocated space in
* memory, which contains a copy of the string given as a parameter
* @str: - Char string to be copied
* Return: Char pointer to the duplicated string. Return NULL if insufficient
* memory was available to array or if array is NULL
*/
char *_strdup(char *str)
{
char *dup;
int i, l;
if (str == NULL)
return (NULL);
l = 0;
for (i = 0; str[i] != '\0'; i++)
l++;
dup = malloc(sizeof(char) * l + 1);
if (dup == NULL)
return (NULL);
for (i = 0; i < l ; i++)
dup[i] = str[i];
dup[l] = '\0';
return (dup);
}
/**
* _strcat - concatenates two strings
* @dest: string 1
* @src: string 2
* Return: concatenated string with overwritten null byte
*/
char *_strcat(char *dest, char *src)
{
int len = _strlen(dest);
int i;
for (i = 0; src[i] != '\0'; i++)
dest[len + i] = src[i];
dest[len + i] = '\0';
return (dest);
}