-
Notifications
You must be signed in to change notification settings - Fork 18
/
CharArrayAlg.c
855 lines (638 loc) · 22.9 KB
/
CharArrayAlg.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#include "../Headers/CharArrayAlg.h"
#include "../../../System/Utils.h"
#include "../../../DataStructure/Strings/Headers/String.h"
#include "../../../DataStructure/Lists/Headers/Vector.h"
#include "../Headers/ArraysAlg.h"
#include "../../../Unit Test/CuTest/CuTest.h"
int charArrAlgStrcmp(const void *c1, const void *c2) {
return strcmp(c1, c2);
}
/** This function will take two chars arrays,
* then it will check if the second string is a substring from the first string,
* if it was the function will return one (1), other wise it will return zero (0).
*
* Time Complexity: O (n) and n is the length of the first char array.
*
* Space Complexity: O (1).
*
* @param fString the first char array pointer
* @param fLength the length of the first char array (without the '\0' character)
* @param sString the second char array pointer
* @param sLength the length of the second char array (without the '\0' character)
* @return it will return one if the second string is a substring of the first one, other wise it will return zero
*/
int isSubString(const char *fString, int fLength, const char *sString, int sLength) {
if (fString == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "first passed char array", "sub string algorithm");
exit(NULL_POINTER);
#endif
} else if (sString == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "second passed char array", "sub string algorithm");
exit(NULL_POINTER);
#endif
} else if (fLength < 0) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "first passed char array length", "sub string algorithm");
exit(INVALID_ARG);
#endif
} else if (sLength < 0) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return -1;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "second passed char array length", "sub string algorithm");
exit(INVALID_ARG);
#endif
}
if (sLength == 0)
return 1;
for (int i = 0; i <= fLength - sLength; i++) {
if (fString[i] != *sString)
continue;
for (int j = 0; j < sLength; j++) {
if (fString[i + j] != sString[j])
break;
else if (j == sLength - 1 && fString[i + j] == sString[j])
return 1;
}
}
return 0;
}
/** This function will take a char array,
* then it will reverse the char array words.
*
* ex: original char array: "one two three", after reversing: "three two one".
*
* @param charArr the char array pointer
*/
void charArrayReverseWords(char *charArr) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array reverse words");
exit(NULL_POINTER);
#endif
}
Vector *wordsVector = vectorInitialization(5, destroyString, NULL);
vectorAdd(wordsVector, stringInitialization(5));
char *charArrStartPointer = charArr;
while (*charArr != '\0') {
if (*charArr == ' ') {
while (*(charArr + 1) == ' ')
charArr++;
if (stringGetLength(vectorGet(wordsVector, vectorGetLength(wordsVector) - 1)) != 0)
vectorAdd(wordsVector, stringInitialization(5));
} else
stringAppendChar((String *) vectorGet(wordsVector, vectorGetLength(wordsVector) - 1), *charArr);
charArr++;
}
for (int i = vectorGetLength(wordsVector) - 1; i >= 0; i--) {
String *currentWord = vectorGet(wordsVector, i);
for (int j = 0; j < stringGetLength(currentWord); j++)
*charArrStartPointer++ = stringGet(currentWord, j);
if (i != 0 && stringGetLength(currentWord) != 0)
*charArrStartPointer++ = ' ';
else
*charArrStartPointer = '\0';
}
destroyVector(wordsVector);
}
/** This function will take an char array,
* then it will removes characters specified in the second array of characters parameter,
* from the beginning of a first array of characters parameter.
*
* ex of specialCharacters array: " \t\n", the function will remove any ' ', '\t', and '\n' characters from the beginning.
*
* @param charArr the characters array pointer
* @param specialCharacters the special characters array pointer
*/
void charArrTrimStartC(char *charArr, char *specialCharacters) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array custom trim start");
exit(NULL_POINTER);
#endif
} else if (specialCharacters == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "special character array", "char array custom trim start");
exit(INVALID_ARG);
#endif
}
while (*charArr != '\0') {
if (charArrContainsChar(specialCharacters, *charArr)) {
char *tempPointer = charArr;
do {
*tempPointer = *(tempPointer + 1);
} while (*tempPointer++ != '\0');
} else
break;
}
}
/** This function will take an char array,
* then it will remove ' ', '\t' and '\n' characters,
* from the beginning of the array of characters.
*
*
* @param charArr the characters array pointer
*/
void charArrTrimStart(char *charArr) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array trim start");
exit(NULL_POINTER);
#endif
}
charArrTrimStartC(charArr, " \n\t");
}
/** This function will take an char array,
* then it will removes characters specified in the second array of characters parameter,
* from the end of the first array of characters parameter.
*
* ex of specialCharacters array: " \t\n", the function will remove any ' ', '\t', and '\n' characters from the end.
*
* @param charArr the characters array pointer
* @param specialCharacters the special characters array pointer
*/
void charArrTrimEndC(char *charArr, char *specialCharacters) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array custom trim end");
exit(NULL_POINTER);
#endif
} else if (specialCharacters == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "special character array", "char array custom trim end");
exit(INVALID_ARG);
#endif
}
char *startPointer = charArr;
while (*charArr != '\0')
charArr++;
while (startPointer - sizeof(char) != --charArr && charArrContainsChar(specialCharacters, *charArr))
*charArr = '\0';
}
/** This function will take an char array,
* then it will remove ' ', '\t' and '\n' characters,
* from the end of the array of characters.
*
*
* @param charArr the characters array pointer
*/
void charArrTrimEnd(char *charArr) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array trim end");
exit(NULL_POINTER);
#endif
}
charArrTrimEndC(charArr, " \n\t");
}
/** This function will take an char array,
* then it will remove ' ', '\t' and '\n' characters,
* from the beginning and the end of the array of characters.
*
*
* @param charArr the characters array pointer
*/
void charArrTrim(char *charArr) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array trim");
exit(NULL_POINTER);
#endif
}
charArrTrimStartC(charArr, " \n\t");
charArrTrimEndC(charArr, " \n\t");
}
/** This function will take an char array,
* then it will removes characters specified in the second array of characters parameter,
* from the beginning and the end of the first array of characters parameter.
*
* ex of specialCharacters array: " \t\n", the function will remove any ' ', '\t', and '\n' characters from the beginning and the end.
*
* @param charArr the characters array pointer
* @param specialCharacters the special characters array pointer
*/
void charArrTrimC(char *charArr, char *specialCharacters) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array custom trim");
exit(NULL_POINTER);
#endif
} else if (specialCharacters == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "special character array", "char array custom trim");
exit(INVALID_ARG);
#endif
}
charArrTrimStartC(charArr, specialCharacters);
charArrTrimEndC(charArr, specialCharacters);
}
/** This function will return one (1) if the passed characters is a part of the passed character array,
* other wise if will return zero (0).
*
* @param charArr the char array pointer
* @param c the character value, that the function will search for
* @return it will return one if the character value exist in the char array, other wise it will return zero
*/
int charArrContainsChar(char *charArr, char c) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array contains char");
exit(NULL_POINTER);
#endif
}
while (*charArr != '\0') {
if (*charArr++ == c)
return 1;
}
return 0;
}
/** This function will take an array of characters,
* and a special characters array, then it will remove every special character from the first passed char array.
*
* ex of specialCharacters array: " \t\n", the function will remove any ' ', '\t', ' ' character from the char array.
*
* @param charArr the char array pointer
* @param specialCharactersToRemove the special characters array pointer
*/
void charArrRemoveCharacters(char *charArr, char *specialCharactersToRemove) {
if (charArr == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array remove character");
exit(NULL_POINTER);
#endif
} else if (specialCharactersToRemove == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "special character to remove array", "char array remove character");
exit(INVALID_ARG);
#endif
}
while (*charArr != '\0') {
if (charArrContainsChar(specialCharactersToRemove, *charArr)) {
char *tempPointer = charArr;
do {
*tempPointer = *(tempPointer + 1);
} while (*tempPointer++ != '\0');
} else
charArr++;
}
}
/** This function will take a char array,
* then it will check if the array is an integer number,
* and if it was the function will return one (1), other wise it will return zero (0).
*
* @param string the char array pointer
* @return it will return one if the char array is integer other wise it will return zero
*/
int isInteger(char *string) {
if (string == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "passed char array", "isInteger function");
exit(NULL_POINTER);
#endif
}
if (*string == '\0')
return 0;
char *tempPointer = string;
while (*tempPointer != '\0') {
if (tempPointer == string && (*tempPointer == '-' || *tempPointer == '+')) {
tempPointer++;
continue;
} else if (!(*tempPointer >= '0' && *tempPointer <= '9'))
return 0;
tempPointer++;
}
return 1;
}
/** This function will check if the passed char array is a floating point number,
* then it will return one (1), if it was a valid floating point number, other wise it will return zero (0).
*
* @param string the char array pointer
* @return
*/
int isFloatingPointNum(char *string) {
if (string == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "passed char array", "isFloatingPointNum function");
exit(NULL_POINTER);
#endif
}
if (*string == '\0')
return 0;
short dotFlag = 0;
char *tempPointer = string;
while (*tempPointer != '\0') {
if (tempPointer == string && (*tempPointer == '-' || *tempPointer == '+')) {
tempPointer++;
continue;
} else if (*tempPointer == '.') {
if (dotFlag == 0)
dotFlag = 1;
else
return 0;
} else if (!(*tempPointer >= '0' && *tempPointer <= '9'))
return 0;
tempPointer++;
}
return 1;
}
/** This function will take an char array pointer,
* then it will return the sum of the ASCII value of the array characters.
*
* @param ch the char array pointer
* @return it will return the sum of the ASCII value of the array characters.
*/
int charArrSumASCII(char *ch) {
if (ch == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array sum ASCII");
exit(NULL_POINTER);
#endif
}
int ASCIISum = 0;
while (*ch != '\0')
ASCIISum += *ch++;
return ASCIISum;
}
/** This function will take an char array pointer,
* then it will return the sum of the ASCII value of the array characters.
*
* Note: this function will be useful to use in the hash set and hash map data structures.
*
* @param ch the char array pointer
* @return it will return the sum of the ASCII value of the array characters.
*/
int charArrHashFun(const void *ch) {
return charArrSumASCII((char *) ch);
}
/** This function will take a char array
* then it will allocate a new one and copy the original char array into the new one,
* and finally return the new allocated char array.
*
* @param ch the char array pointer
* @param length the length of the char array
* @return it will return the new allocated char array pointer
*/
char *generateCharPointerP(char *ch, int length) {
if (ch == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return NULL;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "generate char pointer function");
exit(NULL_POINTER);
#endif
} else if (length < 0) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "char array length", "generate char pointer function");
exit(INVALID_ARG);
#endif
}
char *newCh = (char *) malloc(sizeof(char) * (length + 1));
if (newCh == NULL) {
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "new char pointer", "generate char pointer function");
exit(FAILED_ALLOCATION);
}
strcpy(newCh, ch);
return newCh;
}
/** this function will allocate a new char array pointer,
* the assign it's value with the passed char.
*
* @param c the char value
* @return it will return the new allocated char array
*/
char *generateCharPointerC(char c) {
char *newCh = (char *) malloc(sizeof(char) * 2);
if (newCh == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return NULL;
#else
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "new char pointer", "generate char pointer function");
exit(FAILED_ALLOCATION);
#endif
}
*newCh = c;
*(newCh + 1) = '\0';
return newCh;
}
/** This function will take a character,
* then it will return one (1) if the character is an alphabet, other wise it will return zero (0).
* @param c the char value
* @return it will return one if the character is an alphabet, other wise it will return zero.
*/
int isAlphabetC(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
/** This function will take a character pointer,
* then it will return one (1) if the pointer value is an alphabet, other wise it will return zero (0).
*
* @param c the char value
* @return it will return one if the pointer value is an alphabet, other wise it will return zero.
*/
int isAlphabetP(const char *c) {
if (c == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return -1;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "character", "is alphabet function");
exit(NULL_POINTER);
#endif
}
return (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z');
}
/** This function will take two characters pointers,
* then it will compare the two characters.
*
* The function will return negative number if the first character is bigger in ASCII,
* zero if they are equal, and positive number of the first character bigger.
*
* @param c1 the first character pointer
* @param c2 te second character pointer
* @return it will return negative number if the first character is bigger in ASCII, zero if they are equal, and positive number of the first character bigger
*/
int charComparatorP(const void *c1, const void *c2) {
return *(char *) c1 - *(char *) c2;
}
/** This function will take two characters,
* then it will compare the two characters.
*
* The function will return negative number if the first character is bigger in ASCII,
* zero if they are equal, and positive number of the first character bigger.
*
* @param c1 the first character value
* @param c2 the second character value
* @return it will return negative number if the first character is bigger in ASCII, zero if they are equal, and positive number of the first character bigger
*/
int charComparator(char c1, char c2) {
return c1 - c2;
}
/** This function will take a char array,
* then it will split it by the passed split characters array.
*
* ex of split characters array: " \t\n", the function will split the string when ever it see ' ', '\t', or '\n' characters.
*
* @param string the char array pointer
* @param splitCharacters the split characters array pointer
* @return it will return a vector contains a strings after split
*/
Vector *charArrSplitS(char *string, char *splitCharacters) {
if (string == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return NULL;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array split function");
exit(NULL_POINTER);
#endif
} else if (splitCharacters == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "split characters array", "char array split function");
exit(INVALID_ARG);
#endif
}
Vector *wordsVector = vectorInitialization(5, destroyString, stringCompareS);
vectorAdd(wordsVector, stringInitialization(5));
while (*string != '\0') {
if (charArrContainsChar(splitCharacters, *string)) {
if (stringGetLength(vectorGet(wordsVector, vectorGetLength(wordsVector) - 1)) != 0)
vectorAdd(wordsVector, stringInitialization(5));
} else
stringAppendChar(vectorGet(wordsVector, vectorGetLength(wordsVector) - 1), *string);
string++;
}
if (stringGetLength(vectorGet(wordsVector, vectorGetLength(wordsVector) - 1)) == 0)
vectorRemoveAtIndex(wordsVector, vectorGetLength(wordsVector) - 1);
return wordsVector;
}
/** This function will take a char array,
* then it will split it by the passed split characters array.
*
* ex of split characters array: " \t\n", the function will split the string when ever it see ' ', '\t', or '\n' characters.
*
* @param string the char array pointer
* @param splitCharacters the split characters array pointer
* @return it will return a vector contains a char arrays after split
*/
Vector *charArrSplitC(char *string, char *splitCharacters) {
if (string == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return NULL;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "char array split function");
exit(NULL_POINTER);
#endif
} else if (splitCharacters == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "split characters array", "char array split function");
exit(INVALID_ARG);
#endif
}
Vector *wordsVector = vectorInitialization(5, free, charArrAlgStrcmp);
String *currentWord = stringInitialization(10);
while (*string != '\0') {
if (charArrContainsChar(splitCharacters, *string)) {
if (stringGetLength(currentWord) != 0) {
vectorAdd(wordsVector, stringToCharArray(currentWord));
clearString(currentWord);
}
} else
stringAppendChar(currentWord, *string);
string++;
}
if (stringGetLength(currentWord) != 0)
vectorAdd(wordsVector, stringToCharArray(currentWord));
destroyString(currentWord);
return wordsVector;
}
/** This function will take a char array,
* then it will return the most repeated character.
*
* @param string the char array pointer
* @return it will return the most repeated character
*/
char mostRepeatedCharacter(char *string) {
if (string == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return '\0';
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "char array", "most repeated character function");
exit(NULL_POINTER);
#endif
}
if (*string == '\0')
return '\0';
return *(char *) mostFrequentArrValueH(string, (int) strlen(string), sizeof(char), charComparatorP, charArrHashFun);
}