-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter_function.c
More file actions
107 lines (98 loc) · 1.69 KB
/
Copy pathcharacter_function.c
File metadata and controls
107 lines (98 loc) · 1.69 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
96
97
98
99
100
101
102
103
104
105
106
107
#include "main.h"
/**
* print_char - Function that print char
*
* @c: Fetches the char to printed to standard output
*
* Return: Always 0 (on sucess);
*/
char *print_char(char c)
{
char *string;
string = malloc(sizeof(char) * 2);
if (string == NULL)
{
return (NULL);
}
string[0] = c;
string[1] = '\0';
return (string);
}
/**
* print_string - Function that print integer to standard output
*
* @s: Pointer that fetches the string arguement
*
* Return: The string to stardard.out
*/
char *print_string(char *s)
{
char *string;
int i;
int lenght = 0;
if (s == NULL)
return (s = "(null)");
for (i = 0; s[i] != '\0'; i++)
{
lenght++;
}
string = malloc(sizeof(char) * (lenght + 1));
if (string == NULL)
{
return (NULL);
}
for (i = 0; i < lenght; i++)
{
string[i] = s[i];
}
string[lenght] = '\0';
return (string);
}
/**
* print_string_ - print a string and represent all non printable characters
* with \xand its ascii value in hexadecimal
* @s: a pointer to the string
*
* Return: the new string
*/
char *print_string_(char *s)
{
int i, j = 0, k, lenght = 0;
char *string, *hex;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] < 32 || s[i] >= 127)
lenght += 4;
else
lenght++;
}
string = (char *)malloc(sizeof(char) * (lenght + 1));
if (string == NULL)
return (NULL);
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] < 32 || s[i] >= 127)
{
string[j] = '\\';
j++;
string[j] = 'x';
hex = hex_conversion(s[i], 'X');
if (strlen(hex) < 2)
{
j++;
string[j] = '0';
}
for (k = 0; hex[k] != '\0'; k++)
{
j++;
string[j] = hex[k];
}
free(hex);
}
else
string[j] = s[i];
j++;
}
string[j] = '\0';
return (string);
}