forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringsA.c
116 lines (92 loc) · 1.74 KB
/
stringsA.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
#include "header.h"
/**
* _strncat - concatenates from src string to dest string
* @dest: destination string
* @src: source string
* @n: number of bytes to concatenate
*
* Return: pointer to destination
*/
char *_strncat(char *dest, char *src, int n)
{
int i = 0, j = 0;
while (dest[i] != '\0')
i++;
while (j < n && src[j] != '\0')
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
return (dest);
}
/**
* _strcmp - compares string
* @s1: first string
* @s2: second string
*
* Return: difference between two ascii valuves
*/
int _strcmp(const char *s1, const char *s2)
{
int i;
i = 0;
while (TRUE)
{
if (s1[i] != s2[i])
return (s1[i] - s2[i]);
if (s1[i] == '\0' || s2[i] == '\0')
break;
i++;
}
return (0);
}
/**
* _strlen - finds and returns length of string
* @str: string to find length
*
* Return: length of string
*/
unsigned int _strlen(const char *str)
{
int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
/**
* _strdup - takes a string and copies to another a new memory location
* @str: string to copy
*
* Return: pointer to copied string
*/
char *_strdup(char *str)
{
unsigned int len, j;
char *ptrstring;
if (str == NULL)
return (NULL);
len = _strlen(str);
ptrstring = safe_malloc((len + 1) * sizeof(char));
for (j = 0; j < len; j++)
ptrstring[j] = str[j];
ptrstring[j] = '\0';
return (ptrstring);
}
/**
* _strncmp - checks if 2 strings are of equal value and length
* @s1: first string
* @s2: second string
* @n: number of bytes to compare
*
* Return: difference of first chars of diff value or 0 on success
*/
int _strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int j;
for (j = 0; j < n; j++)
if (s1[j] != s2[j])
return (s1[j] - s2[j]);
return (0);
}