-
Notifications
You must be signed in to change notification settings - Fork 0
/
piscine.h
1158 lines (993 loc) · 23.8 KB
/
piscine.h
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***************************************************************************** */
/* */
/* // // //+## /+| ||+##+\\ //+## /+\ ::: :::::::: */
/* // // // //|| || \\ // // \\ :+: :+: :+: */
/* // // // // || || // // // \\ +:+ +:+ +:+ */
/* //+#+// /+#+ // || || // /+#+ //+##+// +#+ +:+ +#+ */
/* // // // /+##+|| || // // //\\ +#+#+#+#+#+ +#+ */
/* // // // // || || // // // \\ #+# #+# */
/* // // //+## // || ||#// //+#+ // \\ ### ########.fr */
/* |10 |25 |50 nborbiuk.C |82
/* ***************************************************************************** */
#include <unistd.h> //
#include <stdlib.h> //
/*************************/
void ft_putchar(char c) //displays a character in the standard output;
{
write(1, &c, 1);
}
/*************************/
void ft_putstr(char *str) //displays string in the standard output;
{
int i;
i = 0;
while (str[i] != '\0')
{
ft_putchar(str[i]);
i++;
}
}
/*************************/
void ft_putnbr(int nb) //displays a number as a string;
{
if (nb < 0)
{
ft_putchar('-');
if (nb == -2147483648)
{
ft_putchar('2');
nb = nb % 1000000000;
}
nb = -nb;
}
if (nb >= 0 && nb <= 9)
ft_putchar(48 + nb);
else
{
ft_putnbr(nb / 10);
ft_putnbr(nb % 10);
}
}
/*************************/
int ft_atoi(char *str) //returns the number from the string;
{
int i;
int minus;
int res;
i = 0;
minus = 0;
res = 0;
while (str[i] >= 1 && str[i] <= 32)
i++;
if (str[i] == '-')
minus = 1;
if (str[i] == '-' || str[i] == '+')
i++;
while (str[i] >= '0' && str[i] <= '9')
{
res = res * 10;
res = res + str[i] - 48;
i++;
}
if (minus)
return (-res);
else
return (res);
}
/*************************/
char *ft_strcpy(char *dest, char *src) //returns copy of null-terminated string;
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = src[i];
return (dest);
}
/*************************/
char *ft_strncpy(char *dest, char *src, unsigned int n) //returns copy of null-terminated string by n-symbol;
{
unsigned int i;
i = 0;
while (src != '\0' && i < n)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
/*************************/
char *ft_strstr(char *str, char *to_find) //returns the pointer to the place of entry "to_find" in "str";
{
int i;
int j;
int k;
i = 0;
if (to_find[0] == '\0')
return (str);
while (str[i] != '\0')
{
k = i;
j = 0;
while (str[k] == to_find[j])
{
k++;
j++;
if (to_find[j] == '\0')
return (&str[i]);
}
i++;
}
return (NULL);
}
/*************************/
int ft_strcmp(unsigned char *s1, unsigned char *s2) //returns the difference of not the same characters in the string;
{
int i;
i = 0;
while (s1[i] == s2[i] && s1[i] != '\0')
i++;
return (s1[i] - s2[i]);
}
/*************************/
int ft_strncmp(char *s1, char *s2, unsigned int n) //returns the difference of not the same characters in the string to the n-character;
{
int i;
i = 0;
while (s1[i] == s2[i] && s1[i] != '\0' && n > 0)
{
i++;
n--;
}
return (s1[i] - s2[i]);
}
/*************************/
char *ft_strupcase(char *str) //the function returns a string in which all upercase letters;
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 97 && str[i] <= 122)
str[i] = (str[i] - 32);
i++;
}
return (str);
}
/*************************/
char *ft_strlowcase(char *str) //the function returns a string in which all lowercase letters;
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 65 && str[i] <= 90)
str[i] = (str[i] + 32);
i++;
}
return (str);
}
/*************************/
char *ft_strcapitalize(char *str) //the function returns a string in which all words begin with a capital letter;
{
int i;
i = 0;
str = ft_strlowcase(str);
if (str[i] >= 97 && str[i] <= 122)
{
str[i] = str[i] - 32;
i++;
}
while (str[i] != '\0')
{
if ((str[i - 1] >= 1 && str[i - 1] <= 47 || str[i - 1] >= 58 && str[i - 1] <= 64) && str[i] >= 97 && str[i] <= 122)
str[i] = str[i] - 32;
i++;
}
return (str);
}
/*************************/
int ft_str_is_alpha(char *str) //the function checks whether the string contains only alphabetic characters;
{
int i;
i = 0;
if (str[i] == '\0')
return (1);
while (str[i] != '\0')
{
if (str[i] >= 48 && str[i] <= 57)
i++;
else if (str[i] >= 65 && str[i] <= 90)
i++;
else if (str[i] >= 97 && str[i] <= 122)
i++;
else
return (0);
}
return (1);
}
/*************************/
int ft_str_is_numeric(char *str) //the function checks whether the string contains only digits;
{
int i;
i = 0;
while (str[i] != '\0')
{
if (!(str[i] >= 48 && str[i] <= 57))
return (0);
i++;
}
return (1);
}
/*************************/
int ft_str_is_lowercase(char *str) //the function checks whether the string contains only lowercase letters;
{
int i;
i = 0;
if (str[i] == '\0')
return (1);
while (str[i] != '\0')
{
if (!(str[i] >= 97 && str[i] <= 122))
return (0);
i++;
}
return (1);
}
/*************************/
int ft_str_is_uppercase(char *str) //the function checks whether the string contains only upercase letters;
{
int i;
i = 0;
if (str[i] == '\0')
return (1);
while (str[i] != '\0')
{
if (!(str[i] >= 65 && str[i] <= 90))
return (0);
i++;
}
return (1);
}
/*************************/
int ft_str_is_printable(char *str) //the function checks whether the string contains only printable characters;
{
int i;
i = 0;
if (str[i] == '\0')
return (1);
while (str[i] != '\0')
{
if (!(str[i] >= 32 && str[i] <= 127))
return (0);
i++;
}
return (1);
}
/*************************/
int ft_strlen(char *str) //returns the length of the string;
{
int i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
/*************************/
char *ft_strcat(char *dest, char *src) //thr function appends source string to the destination string;
{
int i;
int j;
i = 0;
j = ft_strlen(dest);
while (src[i] != '\0')
{
dest[j] = src[i];
i++;
j++;
}
dest[j] = '\0';
return (dest);
}
/*************************/
char *ft_strncat(char *dest, char *src, int nb) //thr function appends source string to the destination string to n-character;
{
int i;
int j;
i = 0;
j = ft_strlen(dest);
while (src[i] != '\0' && i < nb)
{
dest[j] = src[i];
i++;
j++;
}
dest[j] = '\0';
return (dest);
}
/*************************/
/**FT_PUTNBR_BASE*********/
int check_base(char *base) //chaeck a vailad of base in ft_putnbr_base();
{
int i;
int j;
i = 0;
j = 0;
if (base[0] == '\0' || base[1] == '\0')
return (0);
while (base[i])
{
j = i + 1;
if (base[i] == '+' || base[i] == '-')
return (0);
if (base[i] < 32 || base[i] > 126)
return (0);
while (base[j])
{
if (base[i] == base[j])
return (0);
j++;
}
i++;
}
return (1);
}
void ft_putnbr_base(int nbr, char *base) //the function displays a number in a base system onscreen;
{
int size_base;
int nbr_final[100];
int i;
i = 0;
size_base = 0;
if (check_base(base))
{
if (nbr < 0)
{
nbr = -nbr;
ft_putchar('-');
}
while (base[size_base])
size_base++;
while (nbr)
{
nbr_final[i] = nbr % size_base;
nbr = nbr / size_base;
i++;
}
while (--i >= 0)
ft_putchar(base[nbr_final[i]]);
}
}
/*************************/
/**FT_ATOI_BASE***********/
int ft_index(char c, char *str) //chaeck a vailad of base in ft_atoi_base();
{
int i;
i = 0;
while (str[i] != '\0' && str[i] != c)
i++;
return (i);
}
int ft_atoi_base(char *str, char *base) //the function returns a number in a base system;
{
int i;
int res;
int minus;
int base_len;
i = 0;
res = 0;
minus = 0;
base_len = ft_strlen(base);
if (base_len < 2)
return (0);
while (str[i] >= 1 && str[i] <= 32)
i++;
if (str[i] == '-')
minus = 1;
if (str[i] == '+' || str[i] == '-')
str++;
while (ft_index(str[i], base) < base_len)
{
res = res * base_len;
res = res - ft_index(str[i], base);
i++;
}
if (minus)
return (res);
else
return(-res);
}
/*************************/
/*************************/
void ft_putstr_non_printable(char *str) //ft_putstr, when character is not printable - display a "\hexnumber";
{
int i;
int charecter;
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 32 && str[i] <= 127)
{
ft_putchar(str[i]);
i++;
}
else if (str[i] >= 1 && str[i] <= 31)
{
charecter = str[i];
ft_putchar('\\');
ft_putnbr_base(charecter, "0123456789ABCDEF");
i++;
}
else
i++;
}
}
/*************************/
void ft_swap(int *a, int *b) //the function swaps the value of two integers;
{
int buf;
buf = *a;
*a = *b;
*b = buf;
}
/*************************/
char *ft_strdup(char *s1) //returns identical string;
{
int i;
int len;
char *str;
i = 0;
len = ft_strlen(s1);
str = (char*)malloc(sizeof(*str) * (len + 1));
if (!str)
return (NULL);
while (i < len)
{
str[i] = s1[i];
i++;
}
str[i] = '\0';
return (str);
}
/*************************/
int *ft_range(int min, int max) //returns an array of integers from min to max;
{
int *arr;
int i;
if (min >= max)
{
return (NULL);
}
arr = malloc(sizeof(int) * (max - min));
i = 0;
while (min < max)
{
arr[i] = min;
min++;
i++;
}
return (arr);
}
/*************************/
int ft_ultimate_range(int **range, int min, int max) //returns a size of array of integers from min to max;
{
int *arr;
int i;
if (min >= max)
{
*range = NULL;
return (0);
}
arr = malloc(sizeof(int) * (max - min));
i = 0;
while (min < max)
{
arr[i] = min;
min++;
i++;
}
*range = arr;
return (i);
}
/*************************/
/**FT_CONCAT_PARAMS*******/
char *concat_str(int argc, char **argv) //returns the lengths of all arguments;
{
int i;
int len;
char *str;
i = 1;
len = 0;
while(i < argc)
{
len += ft_strlen(argv[i]);
i++;
}
str = malloc(sizeof(char) * (len + 1));
return (str);
}
char *ft_concat_params(int argc, char **argv) //the function returns one line what is created from arguments;
{
char *str;
int i;
int j;
int n;
str = concat_str(argc, argv);
i = 1;
n = 0;
while (i < argc)
{
j = 0;
while (argv[i][j] != '\0')
{
str[n] = argv[i][j];
j++;
n++;
}
i++;
}
str[n] = '\n';
return str;
}
/*************************/
/**FT_SPLIT)WHITESPACES***/
int ft_count_words(char *str) //the function counts the number of words in a string;
{
int i;
int number;
i = 0;
number = 0;
while (str[i] != '\0')
{
while (str[i] >= 1 && str[i] <= 32)
i++;
if (str[i] >= 33 && str[i] <= 127)
{
while (str[i] >= 33 && str[i] <= 127)
i++;
number++;
}
}
return (number);
}
int len_w(char *str, int i) //returns len of next word;
{
int len;
len = 0;
if (str[i] != '\0' && str[i] >= 33 && str[i] <= 127)
while (str[i] != '\0' && str[i] >= 33 && str[i] <= 127)
{
len++;
i++;
}
return len;
}
char **ft_split_whitespaces(char *str) //the function returns arrays of words from the string;
{
char **res;
int i;
int j;
int k;
i = 0;
j = 0;
if ((res = malloc(sizeof(char*) * (ft_count_words(str) + 1))) == NULL)
return (NULL);
while (str[i] != '\0')
{
while (str[i] != '\0' && str[i] >= 1 && str[i] <= 32)
i++;
if (str[i] != '\0')
{
k = 0;
if ((res[j] = malloc(sizeof(char) * len_w(str, i) + 1)) == NULL)
return (NULL);
while (str[i] != '\0' && str[i] >= 33 && str[i] <= 127)
{
res[j][k] = str[i];
k++;
i++;
}
res[j][k] = '\0';
j++;
}
}
res[j] = NULL;
return (res);
}
/*************************/
/*************************/
void ft_print_word_table(char **tab) //the function displays content of the array of words;
{
int i;
i = 0;
while (1)
{
if (tab[i] == NULL)
break;
ft_putstr(tab[i]);
ft_putchar('\n');
i++;
}
}
/*************************/
void ft_foreach(int *tab, int length, void(*f)(int)) //applis a function on all elements of the array;
{
int i;
i = 0;
while(i < length)
{
f(tab[i]);
i++;
}
}
/*************************/
int *ft_map(int *tab, int length, int(*f)(int)) //applis a function on all elements of the array and returns it;
{
int i;
i = 0;
while (i < length)
{
tab[i] = f(tab[i]);
i++;
}
return (tab);
}
/*************************/
int ft_any(char **tab, int(*f)(char*)) //returns 1 if, passed to the function f, at least one element returns 1;
{
while (**tab++)
{
if (f(*tab) == 1)
return (1);
}
return (0);
}
/*************************/
int ft_count_if(char **tab, int(*f)(char*)) //returns number of elements that return 1, passed to thr function f;
{
int i;
i = 0;
while (**tab++)
{
if(f(*tab) == 1)
i++;
}
return (i);
}
/*************************/
/**FT_SORT_WORDTAB********/
void ft_swap_tab(char **a, char **b) //the function swaps the pointers on the strings;
{
char *tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void ft_sort_wordtab(char **tab) //the function sorts array of strings by ascii table;
{
int i;
i = 0;
while (tab[i + 1] != NULL)
{
if (ft_strcmp(tab[i], tab[i + 1]) > 0)
{
ft_swap_tab(&tab[i], &tab[i + 1]);
i = 0;
}
else
i++;
}
}
/*************************/
/*************************/
char *ft_getstr(int size) //returns an empty string of specified length ("size") including a place for '\0';
{
char *str;
str = malloc(sizeof(char) * (size + 1));
if (size == 0)
return (NULL);
return (str);
}
/*************************/
int ft_putlen_no_spaces(char *str) //returns the length of the string without spaces;
{
int i;
int j;
i = 0;
j = 0;
while (str[i] != '\0')
{
if (str[i] >= 1 && str[i] <= 32)
i++;
else
{
i++;
j++;
}
}
return (j);
}
/*************************/
char *ft_strrev(char *str) //returns the string in reverse order;
{
int i;
int j;
char buf;
i = 0;
j = ft_strlen(str) - 1;
while (i < j)
{
buf = str[i];
str[i] = str[j];
str[j] = buf;
i++;
j--;
}
return (str);
}
/*************************/
char *ft_str_no_spaces(char *str) //returns the string without spaces;
{
int i;
int j;
char *arr;
arr = malloc(sizeof(char) * (ft_putlen_no_spaces(str) + 1));
i = 0;
j = 0;
while (str[i] != '\0')
{
if (str[i] >= 1 && str[i] <= 32)
i++;
else
{
arr[j] = str[i];
i++;
j++;
}
}
arr[j] = '\0';
return (arr);
}
/*************************/
char *ft_sort_char(char *str) //sorts the string by by ascending order;
{
int i;
int j;
int size;
char buf;
i = 0;
size = ft_strlen(str);
while (i < size)
{
j = 0;
while (j < size - i)
{
if (str[j] < str[j - 1])
{
buf = str[j];
str[j] = str[j - 1];
str[j - 1] = buf;
}
j++;
}
i++;
}
return (str);
}
/*************************/
int ft_check_void(char *str, int i) //the function checks if the line is empty after the index;
{ //index = i; i = [1,32];
while (str[i] != '\0')
{
if (!(str[i] >= 1 && str[i] <= 32 && str[i] != '\0'))
return (0);
i++;
}
return (1);
}
/*************************/
int ft_factorial(int nb) //returns the factorial of the specified number;
{
if(nb < 0)
return(0);
if(nb == 0 || nb == 1)
return(1);
return nb * ft_factorial(nb - 1);
}
/*************************/
int ft_itarative_factorial(int nb) //returns the factorial of the specified number;
{
if (nb < 0)
return (-1);
else if (nb == 0)
return (0);
else if (nb == 1)
return (1);
else
{
int i;
int res;
i = 0;
res = 0;
while (i <= nb)
{
res = res * i;
i++;
}
return (res);
}
}
/*************************/
int ft_power(int nb, int power) //returns the number in power;
{
if (power >= 2)
return (nb * ft_power(nb, power - 1));
else if (nb < 0)
return (-1);
else
return (1);
}
/*************************/
int ft_fibonacci(int nb) //returns a number from a Fibonacci numbers;
{
if(nb < 0)
return(-1);
if(nb <=2)
return(nb);
return ft_fibonacci(nb - 1) + ft_fibonacci(nb - 2);
}
/*************************/
int ft_sqrt(int nb) //returns sqrt of number;
{
if (nb < 0)
return (-1);
else
{
int i;
i = 0;
while (i * i < nb)
i++;
if (nb % i == 0)
return (i);
else