-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_2.c
More file actions
126 lines (104 loc) · 1.85 KB
/
str_2.c
File metadata and controls
126 lines (104 loc) · 1.85 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "main.h"
/**
* _strcpy - copies a string to another string
* @dest: the destination
* @src: the string to be copied
* Return: the destination string
*/
char *_strcpy(char *dest, char *src)
{
int i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
/**
* _strdup - duplicates a string
* @str: the string to duplicate
* Return: a copy of the string
*/
char *_strdup(char *str)
{
char *str_copy;
if (str == NULL)
return (NULL);
str_copy = (char *)malloc((_strlen(str) + 1) * sizeof(char));
if (str_copy == NULL)
return (NULL);
_strcpy(str_copy, str);
return (str_copy);
}
/**
* _strchr - search a character in in a string
* @str: the string
* @c: the char to find
* Return: the first occurrence of the char in the string
*/
char *_strchr(const char *str, char c)
{
while (*str != '\0')
{
if (*str == c)
{
return ((char *)str);
}
str++;
}
if (c == '\0')
{
/* Return pointer to null terminator for '\0' */
return ((char *)str);
}
return (NULL);
}
/**
* delete_char - delete a char in a string
* @str: the string
* @ch: the char to delete
* Return: the new string
*/
char *delete_char(char *str, char ch)
{
int length = _strlen(str);
int i, j;
for (i = 0, j = 0; i < length; i++)
{
if (str[i] != ch)
{
str[j++] = str[i];
}
}
str[j] = '\0'; /* Null-terminate the string after deletion*/
return (str);
}
/**
* int_to_string - cast an integer to string
* @num: the number to cast
* Return: a string
*/
char *int_to_string(unsigned long int num)
{
int digits = 1;
unsigned long int temp = num;
char *str;
while (temp /= 10)
{
digits++;
}
str = (char *)malloc((digits + 1) * sizeof(char));
if (str == NULL)
{
return (NULL);
}
str[digits] = '\0';
while (num != 0)
{
str[--digits] = (num % 10) + '0';
num /= 10;
}
return (str);
}