-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.c
More file actions
80 lines (72 loc) · 1.39 KB
/
strings.c
File metadata and controls
80 lines (72 loc) · 1.39 KB
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
#include "shell.h"
/**
* str_num - Convert a number to string format
* @num: Number to be converted
*
* Return: Pointer to string representation of "num"
*/
char *str_num(int num)
{
int num_rev = 0;
int i, digits = 0;
char *num_str = NULL;
if (num == 0)
digits = 1;
else
while (num > 0)
{
digits++;
num_rev *= 10;
num_rev += num % 10;
num /= 10;
}
num_str = malloc(sizeof(char) * (digits + 1));
if (num_str == NULL)
dispatch_error("Error: Coudn't allocate memory for number conversion");
for (i = 0; i < digits; i++)
{
num_str[i] = (num_rev % 10) + '0';
num_rev /= 10;
}
num_str[i] = '\0';
return (num_str);
}
/**
* str_scan - string scanning operation
* @s: string 1
* @c: character to search
* Return: return a pointer to the byte, or
* a null pointer if the byte was not found.
*/
char *str_scan(const char *s, int c)
{
size_t i;
i = 0;
while (s[i])
{
if (s[i] == c)
return ((char *)&s[i]);
i++;
}
if (c == '\0')
return ((char *)(&s[i]));
return (NULL);
}
/**
* n_strcat - concatenate first n chars of s2 to s1 string
* @s1: string 1
* @s2: string 2
* @n: numbers of characters of s2 to concatenate to the s1
* Return: return a pointer to the resulting string dest.
*/
char *n_strcat(char *s1, const char *s2, size_t n)
{
size_t i;
size_t j;
i = strlen(s1);
j = 0;
while (s2[j] && j < n)
s1[i++] = s2[j++];
s1[i] = '\0';
return (s1);
}