-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstr_funcs2.c
134 lines (117 loc) · 2.22 KB
/
str_funcs2.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "shell.h"
/**
* str_mul_cat - concatenates multiple strings
* @dest: destination string
* @str1: first string
* @str2: second string
* Return: concatenated string
*/
char *str_mul_cat(char *dest, char *str2, char *str1)
{
if (dest && str2 && str1)
{
_strcat(dest, str1);
_strcat(dest, str2);
}
return (dest);
}
/**
* get_size - gets number of delimiters
* @str: string to evaluate
* @delim: delimiters
* Return: number of delimiters
*/
size_t get_size(char *str, char delim)
{
size_t x = 0, count = 0;
if (str[x] == '\0')
return (-1);
for (x = 0; str[x]; x++)
{
if (str[x] == delim || str[x + 1] == '\0')
count++;
}
return (count);
}
/**
* print_number - prints a number
* @n: number to print
* @str: str
* Return: number
*/
char *print_number(char *str, int n)
{
unsigned int x;
unsigned int nn = n;
unsigned int y = 1000000000;
int idx = 0, len = 0;
while (nn >= 1)
{
if (nn >= y)
{
if (str)
len = _strlen(str);
x = nn / y;
str = _realloc(str, len, len + 2);
str[idx] = ('0' + x), idx++;
nn = nn - (x * y);
y = y / 10;
while (nn < y)
{
if (str)
len = _strlen(str);
str = _realloc(str, len, len + 1);
str[idx] = ('0' + x), idx++;
y = y / 10;
}
}
else
{
y = y / 10;
}
}
str[idx] = '\0';
return (str);
}
/**
* cat_err - concats error message
* @num: error number
* @argv: argument
* @var: special message
* @token: token
* Return: temp
*/
char *cat_err(char *num, char *argv, char *var, char *token)
{
int x = 0;
char *temp = NULL, *col_space = ": ";
temp = malloc(_strlen(argv) + 3);
for (x = 0; argv[x]; x++)
temp[x] = argv[x];
temp[x] = '\0';
_strcat(temp, col_space);
temp = _realloc(temp, _strlen(temp) + 1,
_strlen(temp) + _strlen(num) + 3);
_strcat(temp, num);
_strcat(temp, col_space);
temp = _realloc(temp, _strlen(temp) + 1,
_strlen(temp) + _strlen(token) + 3);
_strcat(temp, token);
_strcat(temp, col_space);
temp = _realloc(temp, _strlen(temp) + 1,
_strlen(temp) + _strlen(var) + 1);
_strcat(temp, var);
return (temp);
}
/**
* free_2d - frees 2d array
* @arr: 2d array
* Return: void
*/
void free_2d(char **arr)
{
int x = 0;
for (x = 0; arr[x]; x++)
free(arr[x]);
free(arr);
}