-
Notifications
You must be signed in to change notification settings - Fork 0
/
str
897 lines (771 loc) · 23 KB
/
str
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
// -*- C++ -*-
// Created by priyanshu on 10-01-2022.
//
#ifndef STRING_STR_
#define STRING_STR_
#ifndef _GLIBCXX_IOSTREAM
#include "iostream"
#endif
#ifndef _TYPEINFO
#include "typeinfo"
#endif
class str {
private:
// Char array pointer
char* s;
// Function to convert any datatype to str type (internal use only)
template<class T> void tostr(T val);
public:
//Constructors - Below
// Default Constructor
str();
/// @name constructor
/// @prototype str(const char* string_value)
/// @description Constructor to initialise str with char[]
/// @parameter const char* : string_value
str(const char* string_value);
/// @name constructor
/// @prototype str(bool bool_value)
/// @description Constructor to initialise str with bool value
/// @parameter bool : bool_value
explicit str(bool bool_value);
/// @name constructor
/// @prototype str(char char_value)
/// @description Constructor to initialise str with char value
/// @parameter char : char_value
explicit str(char char_value);
/// @name constructor
/// @prototype str(unsigned char unsigned_char_value)
/// @description Constructor to initialise str with unsigned char value
/// @parameter unsigned char : unsigned_char_value
explicit str(unsigned char unsigned_char_value);
/// @name constructor
/// @prototype str(short short_value)
/// @description Constructor to initialise str with short value
/// @parameter short : short_value
explicit str(short short_value);
/// @name constructor
/// @prototype str(unsigned short unsigned_short_value)
/// @description Constructor to initialise str with unsigned short value
/// @parameter unsigned short : unsigned_short_value
explicit str(unsigned short unsigned_short_value);
/// @name constructor
/// @prototype str(int int_value)
/// @description Constructor to initialise str with int value
/// @parameter int : int_value
explicit str(int int_value);
/// @name constructor
/// @prototype str(unsigned int unsigned_int_value)
/// @description Constructor to initialise str with unsigned int value
/// @parameter unsigned int : unsigned_int_value
explicit str(unsigned int unsigned_int_value);
/// @name constructor
/// @prototype str(long long_value)
/// @description Constructor to initialise str with long value
/// @parameter long : long_value
explicit str(long long_value);
/// @name constructor
/// @prototype str(unsigned long unsigned_long_value)
/// @description Constructor to initialise str with unsigned long value
/// @parameter unsigned long : unsigned_long_value
explicit str(unsigned long unsigned_long_value);
/// @name constructor
/// @prototype str(long long long_long_value)
/// @description Constructor to initialise str with long long value
/// @parameter long long : long_long_value
explicit str(long long long_long_value);
/// @name constructor
/// @prototype str(unsigned long long unsigned_long_long_value)
/// @description Constructor to initialise str with unsigned long long value
/// @parameter unsigned long long : unsigned_long_long_value
explicit str(unsigned long long unsigned_long_long_val);
/// @name constructor
/// @prototype str(float float_value)
/// @description Constructor to initialise str with float value
/// @parameter float : float_value
explicit str(float float_value);
/// @name constructor
/// @prototype str(double double_value)
/// @description Constructor to initialise str with double value
/// @parameter double : double_value
explicit str(double double_value);
//Constructors - Above
//Type Casting - Below
/// @description Implementation to typecast from str type to char* type
operator char* ();
/// @description Implementation to typecast from str type to bool type
explicit operator bool ();
/// @description Implementation to typecast from str type to char type
explicit operator char ();
/// @description Implementation to typecast from str type to unsigned char type
explicit operator unsigned char ();
/// @description Implementation to typecast from str type to short int type
explicit operator short ();
/// @description Implementation to typecast from str type to unsigned short int type
explicit operator unsigned short ();
/// @description Implementation to typecast from str type to int type
explicit operator int ();
/// @description Implementation to typecast from str type to unsigned int type
explicit operator unsigned int ();
/// @description Implementation to typecast from str type to long type
explicit operator long ();
/// @description Implementation to typecast from str type to unsigned long type
explicit operator unsigned long ();
/// @description Implementation to typecast from str type to long long type
explicit operator long long ();
/// @description Implementation to typecast from str type to unsigned long long type
explicit operator unsigned long long ();
/// @description Implementation to typecast from str type to float type
explicit operator float ();
/// @description Implementation to typecast from str type to double type
explicit operator double ();
//Type Casting - Above
// Str Operations - Below
/// @name len
/// @prototype int len(void);
/// @description function to compute length of str
/// @parameter none
/// @return int : length of str
int len();
/// @name rev
/// @prototype str rev(bool internal)
/// @description reverse the string, if internal = true then it will update str value with its reverse otherwise it will not modify original str
/// @parameter bool : internal ; true if want to modify original string otherwise false
/// @return str : reversed str
str rev(bool internal);
/// @name (+) concatenation operator
/// @prototype str + str
/// @description concatenate two str
/// @parameter str, str : two str
/// @return str : concatenated string
str operator +(str x);
/// @name (+=) concatenate and update operator
/// @prototype str += str
/// @description append str to right to the end of str to the left of operator
/// @parameter str, str : two str
/// @return none
void operator +=(str x);
/// @name (<<=) copy operator
/// @prototype str <<= str
/// @description copies str to right to str to left of operator
/// @parameter str, str : two str
/// @return none
void operator <<=(str x);
/// @name comparision operator
/// @prototype str == str
/// @description compares the str on left side to str on right side of operator
/// @parameter str, str : two string
/// @return bool : equal(true) or not equal(false)
bool operator ==(str x);
/// @name (==) comparision operator
/// @prototype str == char*
/// @description compares str on left side is equal to char* von right side of operator
/// @parameter str, char* : str and char array pointer
/// @return bool : equal(true) or not equal(false)
bool operator ==(char* x);
/// @name str slicing operator
/// @prototype str(int start, int end, int jump)
/// @description slice the str from start to end(excluding) with jump size that is required
/// @parameter int, int, int : start index, end index, jump size(optional)
/// @return str : sliced string
str operator ()(int start,int end, int jump);
/// @name charAt operator
/// @prototype str[int index]
/// @description returns the character present at inputted index. Supports non negative strictly ascending indexing from left i.e. 0,1,2... and supports negative strictly decreasing index from right side i.e. -1, -2, -3...
/// @parameter int : index
/// @return char : character
char operator [](int x);
/// @name count char
/// @prototype int countChar(char c)
/// @description counts the number of inputted char in str
/// @parameter char : character to count
/// @return int : number of occurrence of character
int countChar(char c); // count occurrence of c in string
/// @name firstIndexOf
/// @prototype int firstIndexOf(char c)
/// @description finds the first occurrence of inputted character
/// @parameter char : character to find
/// @return int :first index of character if found otherwise -1
int firstIndexOf(char c); // returns 1st index of char c in string, if not present returns -1
/// @name lastIndexOf
/// @prototype int lastIndexOf(char c)
/// @description finds the last occurrence of inputted character
/// @parameter char : character to find
/// @return int :last index of character if found otherwise -1
int lastIndexOf(char c); // returns last index of char c in string, if not present returns -1
// Str Operations - Above
//friend functions - Below
friend int len(str s);
friend int len(char* s);
friend str rev(str s);
friend void rev(str* s);
friend int countChar(str s,char c);
friend int firstIndexOf(str s,char c);
friend int lastIndexOf(str s,char c);
friend str read(str message);
friend str readline(str message);
template<class... Str> friend void write(Str... messages);
template<class... Str> friend void writeln(Str... messages);
friend std::istream & operator >>(std::istream &in, str &s);
friend std::ostream & operator <<(std::ostream &ou, str const&s);
//friend functions - Above
};
// Definition Begins Here
int len(str s) {
int c=0;
for(;*(s+c)!='\0';c++);
return c;
}
int len(char* s) {
int c=0;
for(;*(s+c)!='\0';c++);
return c;
}
// Constructor Begins
template<class T> void str::tostr(T val) {
char c = (typeid(val).name())[0];
if(c=='b'){
(this->s) = new char[(val?5:6)];
(this->s) = (char*)((bool)val ? "true" : "false");
} else if(c=='c'){
this->s = new char[2];
*(this->s) = val;
*(this->s+1)='\0';
}else if(c=='h'){
this->s = new char[2];
*(this->s) = (char)val;
*(this->s+1)='\0';
}
else {
T k = val;
int l = 0;
for (int i = 0; k != 0; i++, l++, k /= 10);
l--;
k = val;
if (val < 0) {
k = k * -1;
l++;
(this->s) = new char[l];
*((this->s)) = '-';
} else {
(this->s) = new char[l];
}
*((this->s) + (l + 1)) = '\0';
for (; k != 0; l--) {
char o = (char) (((T)k % 10) + '0');
*((this->s) + l) = o;
k /= 10;
}
}
}
str::str(){
this->s = NULL;
}
str::str(const char *string_value):s{(char*)string_value} {
this->s = new char [::len((char*)string_value)];
int i=0;
for(;*(string_value+i)!='\0';i++){
*(this->s+i)=*(string_value+i);
}
*(this->s+i)='\0';
}
str::str(bool bool_value) {
tostr(bool_value);
}
str::str(char char_value) {
tostr(char_value);
}
str::str(unsigned char unsigned_char_value) {
tostr(unsigned_char_value);
}
str::str(short short_value) {
tostr(short_value);
}
str::str(unsigned short unsigned_short_value) {
tostr(unsigned_short_value);
}
str::str(int int_value) {
tostr(int_value);
}
str::str(unsigned int unsigned_int_value) {
tostr(unsigned_int_value);
}
str::str(long long_value) {
tostr(long_value);
}
str::str(unsigned long unsigned_long_value){
tostr(unsigned_long_value);
}
str::str(long long long_long_value) {
tostr(long_long_value);
}
str::str(unsigned long long unsigned_long_long_value) {
tostr(unsigned_long_long_value);
}
str::str(float float_value){
float k = float_value;
int l = 0, ptAt;
k = k<0 ? k*-1: k;
for (int i = 0; k >=1; i++, l++, k /= 10);
k=float_value;
ptAt=l;
for (int i = 0; (k!=((long long)k)); i++, l++, k *= 10);
if (float_value< 0) {
k = k * -1;
ptAt++;
l++;
(this->s) = new char[l];
*((this->s)) = '-';
} else {
(this->s) = new char[l];
}
*((this->s) + (l+1)) = '\0';
for (; (int)k !=0; l--) {
char c = (char) (((long long )k % 10L) + '0');
if(ptAt==l) {
*((this->s) + l) ='.';
l--;
}
*((this->s) + l) = c;
k /= 10;
}
if(this->len()-1==ptAt) {
*(this->s+ this->len()) = '0';
*(this->s+ this->len()+1) = '\0';
}
}
str::str(double double_value) {
double k = double_value;
int l = 0, ptAt;
k = k<0 ? k*-1: k;
for (int i = 0; k >=1; i++, l++, k /= 10);
k=double_value;
ptAt=l;
for (int i = 0; (k!=((long long)k)); i++, l++, k *= 10);
if (double_value< 0) {
k = k * -1;
ptAt++;
l++;
(this->s) = new char[l];
*((this->s)) = '-';
} else {
(this->s) = new char[l];
}
*((this->s) + (l+1)) = '\0';
for (; (int)k !=0; l--) {
char c = (char) (((long long )k % 10L) + '0');
if(ptAt==l) {
*((this->s) + l) ='.';
l--;
}
*((this->s) + l) = c;
k /= 10;
}
if(this->len()-1==ptAt) {
*(this->s+ this->len()) = '0';
*(this->s+ this->len()+1) = '\0';
}
}
// Constructor Ends
// Friend Functions Below
str rev(str s) {
str op = new char[::len(s)];
int j = 0;
for (int i = ::len(s) - 1; i >= 0; i--, j++) {
*(op + j) = *(s + i);
}
*(op + j) = '\0';
return op;
}
void rev(str *s) {
str op = new char[::len(*s)];
int j = 0;
for (int i = ::len(*s) - 1; i >= 0; i--, j++) {
*(op + j) = *(*s + i);
}
*(op + j) = '\0';
*s = op;
}
int countChar(str s, char c) {
int count=0;
for(int i=0;i<::len(s);i++)
count = *(s+i)==c ? count+1 : count+0;
return count;
}
int firstIndexOf(str s, char c) {
int index=-1;
for(int i=0;i<::len(s);i++){
if(*(s+i)==c){
index = i;
break;
}
}
return index;
}
int lastIndexOf(str s, char c) {
int index=-1;
for(int i=0;i<::len(s);i++)
index = *(s+i)==c ? i : index;
return index;
}
str read(str message=""){
std::cout<<message;
int i=0;
int size=1;
char *pntName=NULL;
char *temp=NULL;
pntName = (char*)malloc( size + 1);
while(1){
size++;
temp = (char*)realloc(pntName, size + 1);
pntName = temp;
if ( ( scanf("%c",&pntName[i])) == 1) {
i++;
pntName[i] = '\0';
if ( pntName[i-1] == ' ' || pntName[i-1]=='\n') {
break;
}
}
else {
break;
}
}
return (str)pntName;
}
str readline(str message=""){
std::cout<<message;
int i=0;
int size=1;
char *pntName=NULL;
char *temp=NULL;
pntName = (char*)malloc( size + 1);
while(1){
size++;
temp = (char*)realloc(pntName, size + 1);
pntName = temp;
if ( ( scanf("%c",&pntName[i])) == 1) {
i++;
pntName[i] = '\0';
if ( pntName[i-1] == '\n') {
break;
}
}
else {
break;
}
}
return (str)pntName;
}
template<class... Str> void write(Str... messages){
(std::cout<<...<<messages)<<" ";
}
template<class... Str> void writeln(Str... messages){
(std::cout<<...<<messages)<<"\n";
}
std::ostream &operator<<(std::ostream &ou, str const&s) {
ou << s.s;
return ou;
}
std::istream &operator>>(std::istream &in, str &x) {
x = new char[::len(x)];
in >>(x.s);
return in;
}
// Friend Functions Above
//Type Casting - Below
str::operator bool() {
if(s=="YES" || s=="yes" || s=="Yes" ||s=="true" || s=="TRUE" || s=="True" || s=="1")
return true;
else if(s=="NO" || s=="no" || s=="No" || s=="false" || s=="FALSE" || s=="False" || s=="0")
return false;
else throw std::invalid_argument("Cannot cast from str to bool");
}
str::operator char(){
if(::len(s)!=1){
throw std::invalid_argument("Cannot Cast String Literal to Char Type");
}
else{
return *s;
}
}
str::operator unsigned char(){
if(::len(s)!=1){
throw std::invalid_argument("Cannot Cast String Literal to Char Type");
}
else{
return (unsigned char)*s;
}
}
str::operator short() {
short n=0,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(short)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to short type");
}
}
return sign_factor*n;
}
str::operator unsigned short() {
unsigned short n=0;
int i = *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned short)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned short type");
}
}
return n;
}
str::operator int() {
int n=0,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=n*10+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to int type");
}
}
return sign_factor*n;
}
str::operator unsigned int() {
unsigned int n=0;
int i = *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned int )n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned int type");
}
}
return n;
}
str::operator long() {
long n=0L,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(long)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to long type");
}
}
return sign_factor*n;
}
str::operator unsigned long() {
unsigned long n=0L;
int i= *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned long)n*10L+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned long type");
}
}
return n;
}
str::operator long long() {
long long n=0L,sign_factor = *(s)=='-' ? -1 : 1;
int i= sign_factor == -1 || *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(long long)n*10LL+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str to long long type");
}
}
return sign_factor*n;
}
str::operator unsigned long long() {
unsigned long long n=0LL;
int i= *s=='+' ? 1 : 0;
for(;i<::len(s)&& *(s+i)!='\n';i++){
if(*(s+i)>='0' && *(s+i)<='9'){
n=(unsigned long long)n*10LL+(*(s+i)-'0');
}
else {
throw std::invalid_argument("Cannot Cast non-numeric str or negative number to unsigned long long type");
}
}
return n;
}
str::operator float() {
float rez = 0, fact = 1;
int point_seen=0,i;
if (*s == '-'){
fact = -1;
};
i = *s == '-' || *s == '+' ? 1 : 0;
for (; i<::len(s)&& *(s+i)!='\n'; i++){
if (*(s+i) == '.'){
point_seen = 1;
continue;
}
int d = *(s+i) - '0';
if (d >= 0 && d <= 9){
if (point_seen) fact /= 10.0f;
rez = rez * 10.0f + (float)d;
} else{
throw std::invalid_argument("Cannot Cast Non numeric str type to float type");
}
}
return rez * fact;
}
str::operator double() {
double rez = 0, fact = 1;
int point_seen=0,i;
if (*s == '-'){
fact = -1;
};
i = *s == '-' || *s == '+' ? 1 : 0;
for (; i<::len(s) && *(s+i)!='\n'; i++){
if (*(s+i) == '.'){
point_seen = 1;
continue;
}
int d = *(s+i) - '0';
if (d >= 0 && d <= 9){
if (point_seen) fact /= 10.0;
rez = rez * 10.0 + (double )d;
} else{
throw std::invalid_argument("Cannot Cast Non numeric str type to double type");
}
}
return rez * fact;
}
//Type Casting - Above
// String Operations - Below
int str::len() {
int c=0;
for(;*(s+c)!='\0';c++);
return c;
}
str str::operator+(str x) {
str op = new char[::len(s)+x.len()+1];
int i=0,j=0;
for(;j<::len(s);j++){
*(op+i)=*(s+j);
i++;
}
for(j=0;j<x.len();j++){
*(op+i)=*(x+j);
i++;
}
*(op+i)='\0';
return op;
}
void str::operator+=(str x) {
str op = new char[::len(s)+x.len()+1];
int i=0,j=0;
for(;j<::len(s);j++){
*(op+i)=*(s+j);
i++;
}
for(j=0;j<x.len();j++){
*(op+i)=*(x+j);
i++;
}
*(op+i)='\0';
s=op;
}
void str::operator<<=(str x) {
str op = new char [x.len()];
int i=0;
for(i=0;i<x.len();i++){
*(op+i)=*(x+i);
}
*(op+i)='\0';
s=op;
}
str str::rev(bool internal= false) {
str op = new char[::len(s)];
int j = 0;
for (int i = ::len(s) - 1; i >= 0; i--, j++) {
*(op + j) = *(s + i);
}
*(op + j) = '\0';
if(internal) {
s=op;
}
return op;
}
str str::operator()(int start, int end, int jump=1) {
str op= new char [((end-start+1)/jump)+1];
int i,j=0;
if(start<=end) {
for (i = start; i <= end; i += jump) {
*(op + j) = *(s + i);
j++;
}
} else{
for (i = start; i >= end; i += jump) {
*(op + j) = *(s + i);
j++;
}
}
*(op+j)='\0';
return op;
}
int str::countChar(char c) {
int count=0;
for(int i=0;i<::len(s);i++)
count = *(s+i)==c ? count+1 : count+0;
return count;
}
int str::firstIndexOf(char c) {
int index=-1;
for(int i=0;i<::len(s);i++){
if(*(s+i)==c){
index = i;
break;
}
}
return index;
}
int str::lastIndexOf(char c) {
int index=-1;
for(int i=0;i<::len(s);i++)
index = *(s+i)==c ? i : index;
return index;
}
char str::operator[](int x) {
if(x<-1*::len(s) || x>::len(s)-1){
throw std::out_of_range("Index our of Range\n");
}
if(x>=0){
return *(s+x);
}
else{
return *(s+::len(s)+x);
}
}
bool str::operator==(str x) {
for(int i=0;i<::len(s);i++){
if(*(s+i)!=*(x+i))
return false;
}
return true;
}
bool str::operator==(char *x) {
for(int i=0;i<::len(s);i++){
if(*(s+i)!=*(x+i))
return false;
}
return true;
}
// String Operations - Above
str::operator char *() {
return s;
}
#endif //STRING_STR_