Repository navigation
Expand file tree
/
Copy pathcstrings.cpp
More file actions
759 lines (634 loc) · 16.4 KB
/
Copy pathcstrings.cpp
File metadata and controls
759 lines (634 loc) · 16.4 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
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
#include<iostream>
#include<string>
using namespace std;
class CString{
private:
char *str;
int size;
public:
CString ( ) ;
CString (char c) ;
CString(char*);
CString ( const CString & );
~CString ( ) ;
int getLenght ( );
void display( ) const ;
int find( char* substr , int start=0 ) const ;
int find( char ch, int start=0 ) const ;
int insert ( int index, char* substr ) ;
int insert ( int index, char ch) ;
CString left ( int count ) const ;
CString right ( int count ) const ;
int remove( int index, int count=1) ;
int remove ( char ch ) ;
void replace( char New ) ;
int replace( char old, char New ) ;
int replace( char* old, char* New ) ;
void trim( ) ;
void trimLeft();
void trimRight ();
void makeUpper ();
void makeLower ( ) ;
void reverse( ) ;
void Resize( int add) ;
CString concat ( const CString& s2 ) const ;
void concatEqual ( const CString& s2 );
void concatEqual ( char * s2 );
int isEqual ( const CString & s2 ) const ;
int isEqual ( char * s2 ) const ;
void input ( ) ;
char & at ( int index) ;
int isEmpty( ) ;
};
CString :: CString ( ) {
size=1;
str = new char[size];
str[0]='\0';
}
//--------------------------------------------------------------------------------------------------
CString :: CString (char c) {
size =2;
str = new char[size];
str[0]=c;
str[1]='\0';
}
//--------------------------------------------------------------------------------------------------
CString :: CString(char* s) {
int len=0;
while(s[len]!='\0'){
len++;
}
size =len +1;
str =new char[size];
for(int i=0; i<size; i++){
str[i]=s[i];
}
}
//--------------------------------------------------------------------------------------------------
CString :: CString ( const CString & ref){
size=ref.size;
str =new char[size];
int i=0;
while(i<size) {
str[i]=ref.str[i];
i++;
}
}
//--------------------------------------------------------------------------------------------------
CString :: ~CString ( ) {
delete [] str;
}
//--------------------------------------------------------------------------------------------------
int CString :: getLenght ( ){
int length=0;
while(str[length]!='\0'){
length++;
}
return length;
}
//--------------------------------------------------------------------------------------------------
void CString :: display( ) const {
int i=0;
while(str[i]!='\0'){
cout<<str[i];
i++;
}
}
//--------------------------------------------------------------------------------------------------
int CString :: find( char* substr , int start ) const {
int length=0;
while(str[length]!='\0'){
length++;
}
int len=0;
while(substr[len]!='\0'){
len++;
}
for( int i=start; i<=length-len; i++){
bool match=true;
for(int j=0; j<len; j++){
if(str[i+j]!=substr[j]){
match= false;
break ;
}
}
if(match){
return i;
}
}
return -1;
}
//--------------------------------------------------------------------------------------------------
int CString :: find( char ch, int start ) const {
int length=0;
while(str[length]!='\0'){
length++;
}
int i=start;
while(i<length){
if(str[i]==ch){
return i;
}
i++;
}
return -1;
}
//--------------------------------------------------------------------------------------------------
int CString :: insert ( int index, char* substr ) {
int length=0;
while(str[length]!='\0'){
length++;
}
int len=0;
while(substr[len]!='\0'){
len++;
}
if(index<0 || index>length){
return 0;
}
char * temp=str;
size= length + len + 1;
str = new char[size];
for(int i=0; i<index; i++){
str[i]=temp[i];
}
for(int i=0; i<len; i++){
str[index+i]=substr[i];
}
for(int i=index; i<=length ; i++){
str[i+len]= temp[i];
}
delete [] temp;
return getLenght();
}
//--------------------------------------------------------------------------------------------------
int CString :: insert ( int index, char ch) {
int length=0;
while(str[length]!='\0'){
length++;
}
size= length +2;
char *temp=str;
str = new char[size];
if(index<0 || index>length){
return 0;
}
for(int i=0; i< index ; i++){
str[i]= temp[i];
}
str[index]=ch;
for(int i=index; i<=length ;i++){
str[i+1]=temp[i];
}
delete [] temp;
int len = getLenght();
return len;
}
//--------------------------------------------------------------------------------------------------
CString CString :: left ( int count ) const {
int length=0;
while(str[length]!='\0'){
length++;
}
if(count>length){
count=length;
}
char * s= new char[count+1];
for(int i =0; i<count; i++){
s[i]=str[i];
}
s[count]='\0';
return CString(s);
}
//--------------------------------------------------------------------------------------------------
CString CString :: right ( int count ) const {
int length=0;
while(str[length]!='\0'){
length++;
}
if(count>length){
count=length;
}
char * s= new char[count+1];
int k=length-count;
for(int i =0 ; i<count; i++){
s[i]=str[k+i];
}
s[count]='\0';
return CString(s);
}
//--------------------------------------------------------------------------------------------------
int CString :: remove( int index, int count) {
int length=0;
while(str[length]!='\0'){
length++;
}
if(index<0 || index >=length || count<=0){
return 0;
}
int k=0;
for(int i=0; i<length; i++){
if(i<index || i>=index+count ){
str[k]= str[i];
k++;
}
}
str[k]='\0';
return k;
}
//--------------------------------------------------------------------------------------------------
int CString :: remove ( char ch ) {
int length=0;
while(str[length]!='\0'){
length++;
}
int count=0;
int j=0;
for(int i=0; i<length; i++){
if(str[i]!=ch){
str[j]=str[i];
j++;
}
else{
count++;
}
}
str[j]='\0';
return count;
}
//--------------------------------------------------------------------------------------------------
void CString :: replace( char New ) {
int length=0;
while(str[length]!='\0'){
length++;
}
for(int i=0; i<length; i++){
str[i]=New;
}
}
//--------------------------------------------------------------------------------------------------
int CString :: replace( char old, char New ) {
int length=0;
while(str[length]!='\0'){
length++;
}
int count=0;
for(int i=0; i<length; i++){
if(str[i]==old){
str[i]=New;
count++;
}
}
return count;
}
//--------------------------------------------------------------------------------------------------
int CString :: replace( char* old, char* New ) {
int oldlength = 0;
while(old[oldlength]!='\0'){
oldlength++;
}
int newlen = 0;
while(New[newlen]!='\0'){
newlen++;
}
int count=0;
int index= find(old,0);
while(index!=-1){
remove(index,oldlength);
insert(index,New);
count++;
index=find(old, index+newlen);
}
return count;
}
//--------------------------------------------------------------------------------------------------
void CString :: trimLeft( ) {
int len= getLenght();
int start=0;
while(start<len && str[start]==' '){
start++;
}
int j=0;
for(int i=start; i<len; i++){
str[j]=str[i];
j++;
}
str[j]='\0';
}
//--------------------------------------------------------------------------------------------------
void CString :: trimRight( ) {
int len= getLenght();
int end=len-1;
while(end>=0 && str[end]==' '){
end--;
}
str[end+1]='\0';
//--------------------------------------------------------------------------------------------------
}
void CString :: trim( ) {
trimLeft();
trimRight();
}
//--------------------------------------------------------------------------------------------------
void CString :: makeUpper (){
int length=0;
while(str[length]!='\0'){
length++;
}
for(int i=0; i<length; i++){
if(str[i]>='a' && str[i]<='z'){
str[i]=str[i]-32;
}
}
}
//--------------------------------------------------------------------------------------------------
void CString :: makeLower (){
int length=0;
while(str[length]!='\0'){
length++;
}
for(int i=0; i<length; i++){
if(str[i]>='A' && str[i]<='Z'){
str[i]=str[i]+32;
}
}
}
//--------------------------------------------------------------------------------------------------
void CString :: reverse( ) {
int length=0;
while(str[length]!='\0'){
length++;
}
for(int i=0; i<length/2; i++){
char temp=str[i];
str[i]=str[length-1-i];
str[length-1-i]=temp;
}
}
//--------------------------------------------------------------------------------------------------
void CString :: Resize( int add) {
int length=0;
while(str[length]!='\0'){
length++;
}
size =length+1;
int newsize =size+add;
char * newstr= new char [newsize];
for(int i=0; i<length; i++){
newstr[i]=str[i];
}
for(int i=length; i<newsize; i++){
newstr[i]='\0';
}
delete[] str;
str=newstr;
size = newsize;
}
//--------------------------------------------------------------------------------------------------
CString CString :: concat ( const CString& s2 ) const {
int len1=0;
while(str[len1]!='\0'){
len1++;
}
int len2=0;
while(s2.str[len2]!='\0'){
len2++;
}
int newsize =len1+len2+1;
char * temp= new char[newsize];
for(int k=0; k<len1; k++){
temp[k]=str[k];
}
for(int l=0; l<len2; l++){
temp[len1+l]=s2.str[l];
}
temp[len1+len2]='\0';
return CString(temp);
}
//--------------------------------------------------------------------------------------------------
void CString :: concatEqual ( const CString& s2 ){
int len1=getLenght();
int len2=0;
while(s2.str[len2]!='\0'){
len2++;
}
char * temp= new char[len1+len2+1];
for(int k=0; k<len1; k++){
temp[k]=str[k];
}
for(int l=0; l<len2; l++){
temp[len1+l]=s2.str[l];
}
temp[len1+len2]='\0';
str=temp;
size = len1+len2+1;
delete[] str;
}
//--------------------------------------------------------------------------------------------------
void CString ::concatEqual ( char * s2 ){
int len1=getLenght();
int len2=0;
while(s2[len2]!='\0'){
len2++;
}
int newsize=len1+len2+1;
char * temp=new char [newsize];
for(int i=0; i<len1; i++){
temp[i]=str[i];
}
for(int i=0; i<len2; i++){
temp[len1+i]=s2[i];
}
temp[len1+len2]='\0';
delete [] str;
str=temp;
size=newsize;
}
//--------------------------------------------------------------------------------------------------
int CString :: isEqual ( const CString & s2 ) const {
int i=0;
while(str[i]!='\0' && s2.str[i] !='\0'){
if(str[i]!=s2.str[i]){
return str[i] - s2.str[i];
i++;
}
}
return str[i] - s2.str[i];
}
//--------------------------------------------------------------------------------------------------
int CString :: isEqual ( char * s2 ) const {
int i=0;
while(str[i]!='\0' && s2[i]!='\0'){
if(str[i]!=s2[i]){
return str[i] - s2[i];
i++;
}
}
return str[i] - s2[i];
}
//--------------------------------------------------------------------------------------------------
void CString:: input ( ) {
delete [] str;
size=100;
str= new char[size];
cin.getline(str,size);
}
//--------------------------------------------------------------------------------------------------
char & CString:: at ( int index) {
int length= getLenght();
if(index<0 || index>length){
cout<<" error"<<endl;
}
return str[index];
}
//--------------------------------------------------------------------------------------------------
int CString:: isEmpty( ) {
if(str[0]=='\0'){
return 0;
}
return 1;
}
int main(){
CString s1;
CString s2('a');
cout<<"parameterized constructor: ";
s2.display();
cout<<endl;
cout<<endl;
CString s3("apple");
cout<<"conversion constructor: ";
s3.display();
cout<<endl;
cout<<endl;
CString s4(s2);
cout<<"copy constructor: ";
s4.display();
cout<<endl;
cout<<endl;
cout<<" length of s2 is: "<<s2.getLenght()<<endl;
cout<<endl;
cout<< " char l in s3 is at : " <<s3.find('l',0)<<endl;
cout<<endl;
char substring[] ="pp";
cout<<"substring pp in s3 is at : "<< s3.find(substring,0);
cout<<endl;
cout<<endl;
CString s5("hello world");
char insert[]="my";
cout<<"the length of string after 'my' is added id :"<<s5.insert(6,insert)<<endl;
cout<<endl;
cout<<"the length of string after ' s ' is added id :"<<s3.insert(6,insert)<<endl;
cout<<endl;
CString left =s5.left(3);
cout<<"range of 3 characters extracted from left side: ";
left.display();
cout<<endl;
cout<<endl;
CString right =s5.right(3);
cout<<"range of 3 characters extracted from right side: ";
right.display();
cout<<endl;
cout<<endl;
cout<<"after removal of a from s3,the length is :" << s3.remove('a') <<endl;
cout<<" s3 becomes: ";
s3.display();
cout<<endl;
cout<<endl;
cout<<"after removal of a from s5,the length is :" << s5.remove(3,4) <<endl;
cout<<" s5 becomes: ";
s5.display();
cout<<endl;
cout<<endl;
cout<<"replacing p in s3 to b : ";
s3.replace('b');
cout<<endl;
cout<<endl;
cout<<"replacing w in s5 to x, number of times are : "<< s5.replace('w','x')<<endl;
cout<<endl;
CString s6("ridaaa");
char oldreplace[]= "a";
char newreplace[]= "u";
cout<<"replacing a in s6 to u, number of times are :"<<s6.replace(oldreplace,newreplace)<<endl;
cout<<" s6 is : ";
s6.display();
cout<<endl;
cout<<endl;
CString s7(" welcome");
cout<<"trim left: ";
s7.trimLeft();
cout<<"s7 becomes: ";
s7.display();
cout<<endl;
cout<<endl;
CString s8("hii ");
cout<<"trim right: ";
s8.trimRight();
cout<<"s8 becomes: ";
s8.display();
cout<<endl;
cout<<endl;
CString s9(" hello ");
cout<<"trim: ";
s9.trim();
cout<<" s9 becomes: ";
s9.display();
cout<<endl;
cout<<endl;
cout<<" uppercasing s9: ";
s9.makeUpper();
cout<<"s9 becomes: ";
s9.display();
cout<<endl;
cout<<endl;
CString s10("ABCD");
cout<<" lowercasing s10: ";
s10.makeLower();
cout<<"s10 becomes: ";
s10.display();
cout<<endl;
cout<<endl;
cout<<"reversing s10: ";
s10.reverse();
cout<<"s10 becomes: ";
s10.display();
cout<<endl;
cout<<endl;
s10.Resize(5);
cout<<" s10 after resizing: ";
s10.display();
cout<<endl;
cout<<endl;
CString s11=s7.concat(s8);
cout<<"after concatentaion s7 & s8 are: ";
s11.display();
cout<<endl;
cout<<endl;
s5.concatEqual(s6);
cout<<"after concatentaion s5 & s6 are: ";
s5.display();
cout<<endl;
cout<<endl;
s6.concat("xyz");
cout<<"after concatentaion of s6 with string are: ";
s6.display();
cout<<endl;
cout<<endl;
cout<<"s3 is equal to s4 ?"<<s3.isEqual(s4)<<endl;
cout<<endl;
cout<<"s2 is equal to abc ?"<<s3.isEqual("abc")<<endl;
cout<<endl;
CString s13;
cout<<" enter a string: "<<endl;
s13.input();
cout<<" second character of s13 : "<<s13.at(1)<<endl;
cout<<endl;
if(s13.isEmpty()){
cout<<" s13 is empty"<<endl;
}
else{
cout<<" s13 is not empty"<<endl;
}
cout<<endl;
cout<<endl;
return 0;
}