-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_helperfun.c
156 lines (143 loc) · 2.47 KB
/
1_helperfun.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "main.h"
#include <stdlib.h>
/**
* our_strdup - Custom implementation of strdup function.
* @s: The string to duplicate.
*
* Return: A pointer to the duplicated string, or NULL if allocation fails.
*/
char *our_strdup(const char *s)
{
size_t len = our_strlen(s) + 1;
char *dup = malloc(len);
if (dup != NULL)
{
our_strcpy(dup, s);
}
return (dup);
}
/**
* our_strcat -attaches the contents of one string to the end
* od another string including the terminating byte
*@src: string to attach
*@dest: string to attach to
*Return: nothing
*/
void our_strcat(char *dest, const char *src)
{
while (*dest != '\0')
{
dest++;
}
while (*src != '\0')
{
*dest = *src;
dest++;
src++;
}
*dest = '\0';
}
/**
*our_printf - function that mimics the standard printf
*
* @format: string
* Return: integer value
*/
int our_printf(const char *format, ...)
{
int i = 0, j = 0;
char *str;
va_list args;
va_start(args, format);
if (format[i] == '%' || (format[i] == '\0' && format[i + 1] == '\0'))
return (-1);
while (format[i])
{
if (format[i] != '%')
{
j += our_putchar(format[i]);
i++;
continue;
}
else if (format[i] == '%' && format[i + 1] == '%')
{
j += our_putchar('%');
}
else if (format[i + 1] == 'c')
{
j += our_putchar(va_arg(args, int));
}
else if (format[i + 1] == 's')
{
str = va_arg(args, char*);
j += write_strings(str);
}
else if (format[i + 1] == 'i' || format[i + 1] == 'd')
j += write_numbers(va_arg(args, int));
else
{
j += our_putchar(format[i]);
i++;
continue;
}
i += 2;
}
va_end(args);
return (j);
}
/**
* write_strings -function that prints strings
* @str: input string to be printed
* Return: number of characters printed
*/
int write_strings(char *str)
{
const char *c = "(null)";
int i = 0;
if (str == NULL)
{
while (*c)
{
i += our_putchar(*c);
c++;
}
}
else
{
while (*str)
{
i += our_putchar(*str);
str++;
}
}
return (i);
}
/**
* write_numbers -function that writes numbers
* @nums: integer to be printed
* Return: numbers printed
*/
int write_numbers(int nums)
{
unsigned int positive_nums;
unsigned int temp;
int length = 1;
int num_chars = 0;
if (nums < 0)
{
num_chars += our_putchar('-');
positive_nums = -nums;
}
else
positive_nums = nums;
temp = positive_nums;
while (temp /= 10)
{
length *= 10;
}
for (; length; length /= 10)
{
num_chars += our_putchar(((positive_nums / length) % 10) + '0');
}
return (num_chars);
}