-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_handler.c
110 lines (83 loc) · 1.5 KB
/
string_handler.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
#include "shell.h"
/**
* str_len - return lent of a str
* @strg: string
* Return: returns lent strg
*/
int str_len(char *strg)
{
int j = 0;
while (strg[j] != '\0')
j++;
return (j);
}
/**
* str_cmp - comper strings.
* @str1: first string.
* @str2: second string.
* Return: 0 if equal,otherwise other.
*/
int str_cmp(char *str1, char *str2)
{
while (*str1)
{
if (*str1 != *str2)
break;
str1++;
str2++;
}
return (*str1 - *str2);
}
/**
* str_cpy - copy strg to anotherr
* @dstn: string one
* @sourc: string two
* Return: string copied
*/
char *str_cpy(char *dstn, char *sourc)
{
int a;
for (a = 0; a <= str_len(sourc); a++)
dstn[a] = sourc[a];
return (dstn);
}
/**
* str_dup - function returns ptr to a new allocatin contains a strg
* given as a parameter
* @strg: - Char strg to cpy
* Return: Char ptr to duplicated string. or NULL
* if no memory
*/
char *str_dup(char *strg)
{
char *dpstr;
int i, l;
if (strg == NULL)
return (NULL);
l = 0;
for (i = 0; strg[i] != '\0'; i++)
l++;
dpstr = malloc(sizeof(char) * l + 1);
if (dpstr == NULL)
return (NULL);
for (i = 0; i < l ; i++)
dpstr[i] = strg[i];
dpstr[l] = '\0';
return (dpstr);
}
/**
* str_cat - concat strgs
* @dstn: strg nbr one
* @sourc: string nbr two
* Return: concat strg
* with overwritten null byt
*/
char *str_cat(char *dstn, char *sourc)
{
int len = str_len(dstn);
int i;
for (i = 0; sourc[i] != '\0'; i++)
dstn[len + i] = sourc[i];
dstn[len + i] = '\0';
return (dstn);
}