-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions_for_strings.c
95 lines (74 loc) · 1.49 KB
/
functions_for_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
#include "shell.h"
/**
* _strncpy - Function that copies a string into other
*@dest: destination of the string
*@src: string to copy
*@n: length of the string
*Return: dest
*/
char *_strncpy(char *dest, char *src, int n)
{
int i;
for (i = 0; i < n && src[i] != '\0'; i++)
dest[i] = src[i];
for ( ; i < n; i++)
dest[i] = '\0';
return (dest);
}
/**
* _strncpyconst - Function that copies a constant string into other
*@dest: destination of the string
*@src: string to copy
*@n: length of the string
*Return: dest
*/
char *_strncpyconst(char *dest, const char *src, int n)
{
int i;
for (i = 0; i < n && src[i] != '\0'; i++)
dest[i] = src[i];
for ( ; i < n; i++)
dest[i] = '\0';
return (dest);
}
/**
* _strlen_const - Function to find the length of a constant string
*@str: string to calculate the length
*Return: the length of the string
*/
unsigned int _strlen_const(const char *str)
{
unsigned int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
/**
* _strlen - Function to find the length of a string
*@str: string to calculate the length
*Return: the length of the string
*/
unsigned int _strlen(char *str)
{
unsigned int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
/**
* _strcmp - Function to compare 2 strings and find if are equal
*@s1: first string to compare
*@s2: second string to compare
*Return: 1 for equal, 0 if not
*/
int _strcmp(char *s1, char *s2)
{
unsigned int i = 0;
while (s1[i] != '\0')
{
if (s1[i] != s2[i])
return (0);
i++;
}
return (1);
}