-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringfuncs.cpp
More file actions
351 lines (280 loc) · 7.14 KB
/
stringfuncs.cpp
File metadata and controls
351 lines (280 loc) · 7.14 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#pragma once
#include "stringfuncs.h"
void strCopy(char * const dest, char const*const source)
{
//alternative 1.
int i = 0;
for (; source[i] != '\0'; i++)
{
dest[i] = source[i];
}
dest[i]='\0';
////alternative 2
//int length = strLength(source);
//for (int i = 0; i <= length; i++)
//{
// dest[i] = source[i];
//}
}
int strLength(char const* const str)
{
int length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
}
int strComp(char const* const str1, char const* const str2)
{
//putting the length of larger string in length variable.
int length = strLength(str1);
if (length < strLength(str2))
{
length = strLength(str2);
}
for (int i = 0; i < length; i++)
{
int diff = str1[i] - str2[i];
if (diff == 0)
continue;
else if (diff < 0)
{
return -1;
}
else return 1;
}
return 0;
}
int findAndReplace(char* const mainString, char const* const oldSubstr, char const* const newSubstr, int const countRemove )
{
int oldSubStrLength = strLength(oldSubstr);
int newSubStrLength = strLength(newSubstr);
int mainStringLength = strLength(mainString);
int occurrancesRemoved = 0;
for (int i = 0; mainString[i] != '\0' && occurrancesRemoved < countRemove; )
{
bool flag = true;
for (int j = 0; j < oldSubStrLength && flag; j++)
{
if (mainString[i + j] != oldSubstr[j])
flag = false;
}
if (flag == true) //replace with new string
{
occurrancesRemoved++;
if (oldSubStrLength == newSubStrLength) //if the string to be replaced is equal to the new string in terms of length
{
for (int j = 0; j < oldSubStrLength; j++)
{
mainString[i + j] = newSubstr[j];
}
}
//if the length of replaced substring is greater than new substring.
else if (oldSubStrLength > newSubStrLength)
{
for (int j = 0; j < newSubStrLength; j++)
{
mainString[i + j] = newSubstr[j];
}
//shifting the remaining characters of main string (oldSubStrLength-newSubStrLength)times left.
int diff = oldSubStrLength - newSubStrLength;
int length = strLength(mainString);
for (int j = i + newSubStrLength; j + diff <= length; j++)
{
mainString[j] = mainString[j + diff];
}
}
else //the length of replaced substring is less than new substring.
{
//copying those characters of main string first that can replace oldSubStr's characters.
for (int j = 0; j < oldSubStrLength; j++)
{
mainString[i + j] = newSubstr[j];
}
int diff = newSubStrLength - oldSubStrLength;
int length = strLength(mainString);
//shifting characters of main string present after currently replaced oldSubStr to right to make room for remaining characters of newSubStr
for (int j = strLength(mainString)+diff; j>=i+oldSubStrLength; j--)
{
mainString[j] = mainString[j-diff];
}
//copying the remaining elements of newSubStr
for (int j = i + oldSubStrLength, k=oldSubStrLength; k<newSubStrLength; j++, k++)
{
mainString[j] = newSubstr[k];
}
}
i += newSubStrLength;
}
else i++;
}
return occurrancesRemoved;
}
void strCat(char* const str1, char const* const str2)
{
int i = strLength(str1);
for (int j = 0; str2[j] != '\0'; i++, j++)
{
str1[i] = str2[j];
}
str1[i] = '\0';
}
void reverseStr(char * const str)
{
for (int i = 0, j = strLength(str)-1; i < j; i++, j--)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
void longToStr(long long const num, char * const dest)
{
long long temp = abs(num);
short i = 0;
do
{
short digit = temp % 10;
temp = temp / 10;
dest[i] = digit + '0';
i++;
} while (temp != 0);
if (num < 0)
{
dest[i] = '-';
i++;
}
dest[i] = '\0';
reverseStr(dest);
}
void intToStr(int const num, char * const dest)
{
int temp = abs(num);
short i = 0;
do
{
short digit = temp % 10;
temp = temp / 10;
dest[i] = digit + '0';
i++;
} while (temp != 0);
if (num < 0)
{
dest[i] = '-';
i++;
}
dest[i] = '\0';
reverseStr(dest);
}
void doubleToStr(double const num, char *const dest, short const decimalPlaces, bool const roundOff)
{
//converting the digits before the decimal point into string.
long long temp = abs(num);
short i = 0;
do
{
short digit = temp % 10;
temp = temp / 10;
dest[i] = digit + '0';
i++;
} while (temp != 0);
if (num < 0)
{
dest[i] = '-';
i++;
}
//reversing the digits.
for (short j = i - 1, k = 0; j > k; j--, k++)
{
char temp = dest[j];
dest[j] = dest[k];
dest[k] = temp;
}
//now converting the decimal part into string and appending to the end of character array
temp = abs(num);
double decimalPart = abs(num) - temp;
short count = 0;
if (decimalPlaces > 0)
{
dest[i] = '.';
i++;
while (count < decimalPlaces)
{
decimalPart = decimalPart * 10;
short digit = decimalPart;
dest[i] = digit + '0';
i++;
count++;
decimalPart = decimalPart - digit;
}
}
//finished converting the required number of digits after decimal point into string.
/*now going to round off the converted number if the first digit of
decimal part which is now not part of the converted decimal is greater than 4.*/
short firstDigitOfDroppedDecimalPart = decimalPart * 10;
if (firstDigitOfDroppedDecimalPart >=5 && roundOff==true)
{
for (short j = i - 1; j >= 0; j--) //round off will begin from the end
{ //and continue till the beginning (right to left)
if (dest[j] == '.')
continue;
if (dest[j] == '-')
break;
if (dest[j] < '9')
{
dest[j]++;
break;
}
if (j == 0 || (j == 1 && dest[0] == '-'))
{
//this part converts number like 9.9 into 10.0 during rounding off.
dest[j] = '0'; //we had a 9 at index j.
//shifting digits one step right so that there is a space for putting 1 (if the first digit of decimal number is 9 then it will be converted to 10 during round off)
for (short k = i - 1; k >= j; k--)
{
dest[k + 1] = dest[k];
}
dest[j] = '1';
i++;
}
else
{
dest[j] = '0'; //if this 9 is not the first digit, then no need to insert 1.
}
}
}
dest[i] = '\0';
/*it is possible to get -0 in the string if the number was -0.9, and
and we passed decimalPlaces=0 with roundOff=false. similarly we
can get -0.00 if the number was -0.009 with decimalPlaces=2 and roundOff=0
In such cases, we will remove - sign.*/
bool flag = false;
if (dest[0] == '-')
{
for (short j = 1; j < i && flag == false; j++)
{
if (dest[j]>='1' && dest[j]<='9')
{
flag = true;
}
}
}
if (flag==false)
findAndReplace(dest, "-", "", 1);
}
int strToInt(char const * const src)
{
int num = 0;
int i = 0;
//ignoring the leading spaces, tabs and nextline characters.
while (src[i] == '\t' || src[i] == '\n' || src[i] == ' ')
{
i++;
}
for ( ;src[i]>='0' && src[i]<='9'; i++)
{
num = num * 10 + (src[i] - '0');
}
return num;
}