-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_string.c
executable file
·266 lines (230 loc) · 8.33 KB
/
new_string.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************/
/*
/* Project Name: String 'Em Up
/* Description: implementations of string library functions
/* File names: new_string.c new_sring.h makefile
/* Date: March, 14, 2014
/* Authors: Timothy Miranda, Till Krischer
/*
/*******************************/
#include <stddef.h>
#include <stdio.h>
#include "new_string.h"
/*
Description: Copies the characters from source into destination.
Parameters: destination - as a char pointer
source - as a char pointer
Returns: pointer to destination
*/
char* new_strcpy(char* destination, const char* source)
{
int i;
//loop through the souce
for(i = 0; source[i]; ++i)
//copy char from source to destination
destination[i] = source[i];
//append null character to destination
destination[i] = '\0';
return destination;
}
/*
Description: Copies the first n characters from source into destination. If the function hits a null character in source before it has copied n characters, the remaining characters are filled with null characters.
Parameters: destination - as a char pointer
source - as a char pointer
n - character-limit as int
Returns: pointer to destination
*/
char* new_strncpy(char* destination, const char* source, size_t n)
{
size_t i;
//loop through source until end of source or until n-th character.
for (i = 0; i < n && source[i] != '\0'; i++)
//copy char from source to destination
destination[i] = source[i];
// if source length < n loop until n
for ( ; i < n; i++)
//fill rest of destination with null-characters
destination[i] = '\0';
return destination;
}
/*
Description: Compares two strings.
Parameters: string1 - as a char pointer
string2 - as a char pointer
Returns: The return value is positive if string1 comes after string2 alphabetically, negative if string1 comes before string2 alphabetically, and 0 if the two strings are equal.
*/
int new_strcmp(const char* string1, const char* string2)
{
int i = 0;
int check = 1;
while(check != 0)
{
if(string1[i] == '\0' && string2[i] == '\0') // if both strings finish here then they must be equal
{
return 0;
}
else if(string1[i] == '\0') // if string 1 finishes before string 2 than it is less than it
{
return -1;
}
else if(string2[i] == '\0')// if string 2 finishes before string 1 than it is greater than it
{
return 1;
}
else if(string1[i] - string2[i] < 0) //if the difference between characters is negative then string1 is less than string 2
{
return string1[i] - string2[i]; // returns a negative number
}
else if(string1[i] - string2[i] > 0)//if the difference between characters is positive then string1 is greater than string 2
{
return string1[i] - string2[i]; // returns a positive number
}
else // when string1[i] == string2[i]
{
i++; // increments i
}
}
}
/*
Description: Compares two strings exactly like new_strcmp(), except comparing at most the first n characters.
Parameters: string1 - as a char pointer
string2 - as a char pointer
n - charater limit as int
Returns: The return value is positive if string1 comes after string2 alphabetically, negative if string1 comes before string2 alphabetically, and 0 if the two strings are equal.
*/
int new_strncmp(const char* string1, const char* string2, size_t n)
{
int i = 0;
while(i < n) // loops n times
{
if(string1[i] == '\0' && string2[i] == '\0') // if both strings finish here then they must be equal
{
return 0;
}
else if(string1[i] == '\0') // if string 1 finishes before string 2 than it is less than it
{
return -1;
}
else if(string2[i] == '\0')// if string 2 finishes before string 1 than it is greater than it
{
return 1;
}
else if(string1[i] - string2[i] < 0) //if the difference between characters is negative then string1 is less than string 2
{
return string1[i] - string2[i]; // returns a negative number
}
else if(string1[i] - string2[i] > 0)//if the difference between characters is positive then string1 is greater than string 2
{
return string1[i] - string2[i]; // returns a positive number
}
else // when string1[i] == string2[i]
{
i++; // increments i
}
}
return 0;
}
/*
Description: Adds the string contained in source to the end of the string contained in destination. The values in destination are modified, but a pointer to destination is also returned.
Parameters: destination - as a char pointer
source - as a char pointer
Returns: pointer to destination
*/
char* new_strcat(char* destination, const char* source)
{
//length of destination
size_t dest_len = new_strlen(destination);
size_t i;
//loop through source
for (i = 0 ; source[i] != '\0' ; ++i)
//append i-th character of souce to destination
destination[dest_len + i] = source[i];
//append null character to destination
destination[dest_len + i] = '\0';
return destination;
}
/*
Description: Adds the string contained in source to the end of the string contained in destination, but adding a maximum of n characters.
Parameters: destination - as a char pointer
source - as a char pointer
n - character-limit as int
Returns: pointer to destination
*/
char* new_strncat(char* destination, const char* source, size_t n)
{
//length of destination
size_t dest_len = new_strlen(destination);
size_t i;
//loop through source until end of souce or until n
for (i = 0 ; i < n && source[i] != '\0' ; ++i)
//append i-th character of souce to destination
destination[dest_len + i] = source[i];
//append null character to destination
destination[dest_len + i] = '\0';
return destination;
}
/*
Description: Returns the number of characters in string before the null character.
Parameters: string - string to measure as ca poiner
Returns: length of string as an int
*/
size_t new_strlen(const char* string)
{
size_t i;
//loop through string and increment i per character
for(i = 0; string[i]; ++i);
return i;
}
/*
Description: Returns a pointer to the first occurrence of character (converted to a char) in string or a NULL pointer if character cannot be found.
Parameters: string - string to search in as char pointer
character - char to look for as char
Returns: pointer to first occurence of "character" in string, or null if character cannot be found
*/
char* new_strchr(const char* string, int character)
{
int i;
//default return is NULL pointer
char* ret = NULL;
//loop through string
for(i = 0; string[i]; ++i)
//if i-th character in string is equal to "character" set return to its address.
if(string[i] == character)
ret = &string[i];
return ret;
}
/*
Description: Returns a pointer to the first occurrence of the string contained in needle in haystack or a NULL pointer if needle cannot be found.
Parameters: haystack - string to search in as char pointer
needle - string to search for as char pointer
Returns: pointer to first character of first occurence of needle, null-pointer is needle is not found.
*/
char* new_strstr(const char* haystack, const char* needle)
{
int i = 0;
int j = 0;
int check = 1; // creates an int that will act as a boolean
char* ret = NULL; // creates a pointer to return
while(haystack[i] != '\0')
{
if(haystack[i] == needle[0]) // checks to see if the current character of haystack is the same as the first character of needle
{
while(needle[j] != '\0' && check !=0) // loops through characters in needle until it ends or if check is set to 0
{
if(needle[j] != haystack[i+j]) // checks to see if the characters aren't the same
{
check = 0; // sets check to 0, ending the loop
}
j++; //increments j
}
if(check) // if check was never changed, then haystack contained needle
{
ret = &haystack[i]; // sets the return value to the first occurance of the string contained in needle in haystack
return ret; // returns that value
}
}
j=0; // resets j to 0
i++; // increments i
}
return ret; // returns null pointer if needle cannot be found
}