-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_string.cpp
More file actions
167 lines (115 loc) · 3.31 KB
/
Copy pathmy_string.cpp
File metadata and controls
167 lines (115 loc) · 3.31 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
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
156
157
158
159
160
161
162
163
164
165
166
167
#include "my_string.hpp"
#include <string.h>
int my_puts (const char *string)
{
if (string == NULL) return EOF;
int index = 0;
for (index; string[index] != '\0'; index++)
{
if (putchar(string[index]) == EOF)
return EOF;
}
if (putchar ('\n') == EOF)
return EOF;
return '\n';
}
// рисунок к строковой функции
char * my_strchr (const char *string, int symbol)
{
if (string == NULL) return NULL;
int index = 0;
for (index; string[index] != '\0'; index++)
{
if (string[index] == symbol) return (char *)string + index;
}
return NULL;
}
size_t my_strlen (const char *string)
{
int index = 0;
for (index; string[index] != '\0'; index++);
return index;
}
char * my_strcpy (char *destination, const char *source)
{
if (destination == NULL or source == NULL)
return NULL;
char *ptr = destination;
for (int index = 0; source[index] != '\0'; index++)
{
destination[index] = source[index];
}
return destination;
}
char * my_strncpy (char *destination, const char *source, size_t len)
{
if (destination == NULL or source == NULL)
return NULL;
for (int index = 0; index < len; index++)
{
if (index <= my_strlen(source))
destination[index] = source[index];
else
destination[index] = '\0';
}
return destination;
}
char * my_strcat( char * destination, const char * source )
{
if (destination == NULL or source == NULL)
return NULL;
int len_dst = my_strlen(destination);
for (int index = 0; source[index] != '\0'; index++)
{
destination[len_dst + index] = source[index];
}
return destination;
}
char * my_strncat (char *destination, const char *source, size_t len)
{
if (destination == NULL or source == NULL)
return NULL;
int len_dst = my_strlen(destination);
for (int index = 0; (source[index] != '\0') and (index < len); index++)
{
destination[len_dst + index] = source[index];
}
return destination;
}
char * my_fgets(char *string, int num, FILE *stream)
{
if (string == NULL or stream == NULL)
return NULL;
int index = 0;
char symbol = '\0';
for (index; (index < num) and (symbol) != '\n'; index++)
{
symbol = fgetc(stream);
if (symbol == EOF) return NULL;
string[index] = symbol;
}
string[index + 1] = '\0';
return string;
}
char * my_strdup(const char *string)
{
if (string == NULL) return NULL;
char * buf = (char *) malloc (my_strlen (string) + 1);
if (buf == NULL) return NULL;
return my_strcpy (buf, string);
}
char * my_getline (FILE *file, char *buf, char delim)
{
if (file == NULL or buf == NULL)
return NULL;
int index = 0;
char symbol = '\0';
char delim = '\n';
for (index ; symbol != delim; index++)
{
symbol = fgetc(file);
if (symbol == EOF) return NULL;
buf[index] = symbol;
}
return buf;
}