-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.c
121 lines (99 loc) · 1.89 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
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "shell.h"
/**
* *_strdup - retrun a pointer to a newly allocated space in memory
* which contains a copy of the string given as a parameter.
* @str: string
* Return: 0
*/
char *_strdup(const char *str)
{
char *m;
int i, size = 0;
if (str == NULL)
return (NULL);
while (*str != '\0')
{
size++;
str++;
}
str = str - size;
/* +1 on the size puts the end of string character*/
m = malloc(sizeof(char) * (size + 1));
if (m == NULL)
return (NULL);
for (i = 0; i <= size; i++)
m[i] = str[i];
return (m);
}
/**
* _strcmp - compares two strings
* @s1: first string to be compared
* @s2: second string to be compared
*
* Return: 1 if true 0 if false
*/
int _strcmp(char *s1, char *s2)
{
int i = 0;
while (s1[i] != '\0' && s2[i] != '\0' && s1[i] == s2[i])
{
i++;
}
return (s1[i] - s2[i]);
}
/**
* _strlen - returns the length of a string
*
* @s: string parameter input
*
* Return: length of string
*/
int _strlen(char *s)
{
int counter;
for (counter = 0; *s != ('\0'); s++)
counter++;
return (counter);
}
/**
* _strcat - Concatenates two strings
* @dest: The destination string
* @src: The source string
*
* Return: A pointer to the resulting string (dest)
*/
char *_strcat(char *dest, char *src)
{
int dest_len = 0;
int i = 0;
/*find the length of dest*/
while (dest[dest_len] != '\0')
dest_len++;
/*append src to dest*/
while (src[i] != '\0')
{
dest[dest_len] = src[i];
dest_len++;
i++;
}
/*Null-terminate the concatenated string*/
dest[dest_len] = '\0';
return (dest);
}
/**
* *_strcpy - copies the string pointed to by src
* @dest: char type string
* @src: char type string
* Description: Copy the string pointed to by pointer 'src' to the
* buffer pointed to by 'dest'
* Return: Pointer to 'dest'
*/
char *_strcpy(char *dest, char *src)
{
int i = -1;
do {
i++;
dest[i] = src[i];
} while (src[i] != '\0');
return (dest);
}