forked from gutudanii/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_funs.c
94 lines (87 loc) · 1.35 KB
/
string_funs.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
#include "shell.h"
/**
* _strcat - append src to dest
* @dest: first string
* @src: string to be appended
* Return: final dest string
*/
char *_strcat(char *dest, char *src)
{
int i;
for (i = 0; dest[i] != '\0'; i++)
{
}
for (i = i; *src; i++)
{
*(dest + i) = *src++;
}
return (dest);
}
/**
* _strlen - return length of string
* @s: char
* Return: int
*/
int _strlen(char *s)
{
int cnt;
for (cnt = 0; *(s + cnt) != '\0'; cnt++)
{
}
return (cnt);
}
/**
* rev_string - reverse_string
* Description: Tool for convert int to string
* @s: string
* Return: void
*/
void rev_string(char *s)
{
int init, end;
char aux;
for (end = 0; *(s + end) != '\0'; end++)
{
};
end--;
for (init = 0; init < end; init++)
{
aux = *(s + init);
*(s + init) = *(s + end);
*(s + end--) = aux;
}
}
/**
* _itoa - convert num to base 10 and return in string
* @num: int
* Return: string
*/
char *_itoa(unsigned int num)
{
char *tool = "0123456789abcdef";
char *new = _calloc(64, 1), *init;
if (!new)
return (NULL);
init = new;
while (num != 0)
{
*new++ = tool[num % 10];
num /= 10;
}
rev_string(init);
return (init);
}
/**
* _strcmp - compare two strings
* @s1: char*
* @s2: char*
* Return: int -1, 1, 0
*/
int _strcmp(char *s1, char *s2)
{
int i;
for (i = 0; s1[i] != '\0'; i++)
if (s1[i] != s2[i])
return (-1);
return (0);
}