-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings1.c
More file actions
120 lines (102 loc) · 1.78 KB
/
strings1.c
File metadata and controls
120 lines (102 loc) · 1.78 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
111
112
113
114
115
116
117
118
119
#include "main.h"
/**
* _strcpy - Copies one string to another
* @dest: DESTINATION
* @src: SOURCE
*
* Return: Pointer to destination
*/
char *_strcpy(char *dest, const char *src)
{
size_t i;
for (i = 0; src[i] != '\0'; i++)
dest[i] = src[i];
dest[i] = '\0';
return (dest);
}
/**
* _strcmp - compares two strings s1 and s2
* @s1: The first string
* @s2: The second string
*
* Return: 0, if strings are equal
* if first string < second string, -1
* if first string > second string, 1
*/
int _strcmp(const char *s1, const char *s2)
{
while (*s1 != '\0' && *s2 != '\0')
{
if (*s1 != *s2)
{
return (*s1 - *s2);
}
s1++;
s2++;
}
return (*s1 - *s2);
}
/**
* _strconcat - Concatenates two strings
* @s1: The first string
* @s2: The second string
*
* Return: A newly allocated string containing the concatenated s1 and s2
*/
char *_strconcat(const char *s1, const char *s2)
{
char *result;
int len1 = _strlen(s1);
int len2 = _strlen(s2);
int i, j;
result = malloc(len1 + len2 + 2);
if (result == NULL)
{
perror("Memory allocation error");
return (NULL);
}
for (i = 0; i < len1; i++)
{
result[i] = s1[i];
}
result[i] = '/';
for (j = 0; j < len2; j++)
{
result[i + 1 + j] = s2[j];
}
result[i + 1 + j] = '\0';
return (result);
}
/**
* _strlen - Returns the length of a string
* @s: The string to measure
*
* Return: The length of the string
*/
int _strlen(const char *s)
{
int len = 0;
while (s[len])
{
len++;
}
return (len);
}
/**
* _strchr - Chceks the equality of two strings
* @s: First string to be checked
* @c: Second string to be checked
*
* Return: @s if the strings are equal
* else NULL is returned
*/
char *_strchr(char *s, char c)
{
while (*s != '\0')
{
if (*s == c)
return (s);
s++;
}
return (NULL);
}