-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.c
More file actions
95 lines (78 loc) · 1.46 KB
/
helpers.c
File metadata and controls
95 lines (78 loc) · 1.46 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "main.h"
/**
* get_func - finds function to use for adding to buffer
* @s: character used to find correct function
* Return: pointer to desired function
*/
char *(*get_func(char s))(va_list)
{
format operations[] = {
{'d', dipr},
{'i', dipr},
{'c', cpr},
{'s', spr}
};
int i = 0;
while (s != operations[i].fmt)
{
i++;
if (i > 3)
return (NULL);
}
return (operations[i].f);
}
/**
* cpstr - count and print each character in a string
* @s: input string to print and count chars
* Return: number of characters in the string.
*/
int cpstr(char *s)
{
int numChars = 0;
while (s[numChars] != '\0')
{
_putchar(s[numChars]);
numChars++;
}
return (numChars);
}
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* _strcat - concatenates two strings
* @s1: first string
* @s2: second string (s2 is added to the end of s1)
* Return: pointer to s1
*/
char *_strcat(char *s1, char *s2)
{
int l1 = _strlen(s1);
int l2 = _strlen(s2);
int i;
for (i = 0; i < l2; i++)
s1[l1 + i] = s2[i];
return (s1);
}
/**
* _strlen - finds the length of a string
* @s: string input
* Return: 0 if s is NULL, integer length (excluding '\0') otherwise
*/
int _strlen(char *s)
{
int len = 0;
if (s == NULL)
return (0);
while (s[len] != '\0')
len++;
return (len);
}