-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_utils.c
76 lines (66 loc) · 1.14 KB
/
str_utils.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
#include "util.h"
/**
* str_len - counts chars in a string
* @str: string to count from
*
* Return: number of characters count
*/
unsigned int str_len(char *str)
{
if (*str != '\0')
return (1 + str_len(str + 1));
return (0);
}
/**
* str_concat - canatenates two strings
* @s1: first string
* @s2: second string
*
* Return: pointer to concated result
*/
char *str_concat(char *s1, char *s2)
{
int len1, len2, i;
char *r, s0[] = {0};
len1 = len2 = i = 0;
if (s1 == NULL)
s1 = s0;
else
len1 = str_len(s1);
if (s2 == NULL)
s2 = s0;
else
len2 = str_len(s2);
len2 += len1;
r = malloc(sizeof(char) * len2 + 1);
while (r != NULL && i < len1)
{
*(r + i) = s1[i];
i++;
}
while (r != NULL && i <= len2)
{
*(r + i) = s2[i - len1];
i++;
}
return (r);
}
/**
* str_diff - compares two strings.
* @s1: first string
* @s2: second string
*
* Return: if both are identical
* or s1 contains s2
* then return 0
* else return difference
*/
int str_diff(char *s1, char *s2)
{
int i = 0;
while (s1[i] - s2[i] == 0 && s2[i] != 0)
i++;
if (s2[i] == 0)
return (0);
return (s1[i] - s2[i]);
}