-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_funcs.c
122 lines (108 loc) · 1.88 KB
/
str_funcs.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
#include "main.h"
/**
* _strlen - string length function
* @str: string to get its length
* Return: length
*/
size_t _strlen(const char *str)
{
size_t length = 0;
while (str[length] != '\0')
{
length++;
}
return (length);
}
/**
* _strcpy - string copy function
* @dest: destination string
* @src: source string
* Return: dest
*/
char *_strcpy(char *dest, const char *src)
{
char *dest_ptr = dest;
while (*src != '\0')
{
*dest_ptr = *src;
dest_ptr++;
src++;
}
/* Add null character at the end */
*dest_ptr = '\0';
return (dest);
}
/**
* _strchr - string character search function
* @str: string to search
* @c: character to find
* Return: NULL
*/
char *_strchr(const char *str, int c)
{
while (*str != '\0')
{
if (*str == c)
{
return ((char *)str);
}
str++;
}
/* Handle the case when the character is not found */
if (c == '\0')
{
return ((char *)str);
}
return (NULL);
}
/**
* _strncmp - string compare with nth times function
* @str1: first string
* @str2: second string
* @n: number of times to compare
* Return: Success (0)
*/
int _strncmp(const char *str1, const char *str2, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
if (*str1 != *str2)
{
return (*str1 - *str2);
}
/* If either string reaches the null character, break the loop */
if (*str1 == '\0' || *str2 == '\0')
{
break;
}
str1++;
str2++;
}
return (0);
}
/**
* _strcat - string concatenation function
* @dest: destination string
* @src: source string
* Return: dest
*/
char *_strcat(char *dest, const char *src)
{
char *dest_ptr = dest;
/* Find the end of the destination string */
while (*dest_ptr != '\0')
{
dest_ptr++;
}
/* Append characters from the source string to the destination string */
while (*src != '\0')
{
*dest_ptr = *src;
dest_ptr++;
src++;
}
/* Add null character at the end */
*dest_ptr = '\0';
return (dest);
}