-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Implementation.cpp
More file actions
145 lines (129 loc) · 3.33 KB
/
Copy pathString_Implementation.cpp
File metadata and controls
145 lines (129 loc) · 3.33 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
// Please define and implement a class called String and provide commands as follows:
// freq: show frequency of each of the distinct characters in the string. (if character count < 1, shows nothing.)
// find str: show string str’s first position in the string. (if str not found, shows -1.)
// del start length: remove length characters beginning at start, and show result.
// chdel c: remove char c in string, and show result.
#include <iostream>
using namespace std;
class String{
public:
friend istream& operator>>(istream& is, String& string);
friend ostream& operator<<(ostream& os, String& string);
friend bool operator==(const String& lhs, const String& rhs);
int Find(String x){
for(int i=0;i<length;i++){
int num=i;
int num2=0;
for(int j=0;j<x.length;j++){
if(array[num]==x.array[j]){
num++;
num2++;
}else if(array[num]!=x.array[j]||num+(x.length-j+1)>length+1){
break;
}
}
if(num2==x.length){
return i;
}
}
return -1;
}
void Delete(int start,int l){
if(length-start-l<0){
return;
}
char *newarray=new char[225];
int i=0;
int j=0;
while(i<start){
newarray[j++]=array[i];
i++;
}
i+=l;
while(i<length){
newarray[j++]=array[i];
i++;
}
array=newarray;
array=new char[225];
copy(newarray,newarray+length,array);
length=j;
}
void chdel(char d){
for(int i=0;i<length;i++){
if(array[i]==d){
Delete(i,1);
}
}
}
void freq(){
int ch[26]={0};
for(int i=0;i<length;i++){
ch[array[i]-'a']++;
}
for(int i=0;i<26;i++){
if(ch[i]>0){
cout<<(char)(i+'a')<<" "<<ch[i]<<endl;
}
}
}
public:
char *array;
int length;
};
istream& operator>>(istream& is, String& string){
string.array=new char[225];
int num=0;
is>>string.array;
int count=0;
while(string.array[num]){
count++;
num++;
}
string.length=count;
return is;
}
ostream& operator<<(ostream& os, String& string){
for(int i=0;i<string.length;i++){
os<<string.array[i];
}
return os;
}
bool operator==(const String& lhs, const String& rhs){
if(lhs.length!=rhs.length){
return false;
}
for(int i=0;i<lhs.length;i++){
if(lhs.array[i]!=rhs.array[i]){
return false;
}
}
return true;
}
int main(int argc, const char * argv[]) {
String s;
cin>>s;
string command;
while(cin>>command){
if(command=="freq"){
s.freq();
}else if(command=="chdel"){
char x;
cin>>x;
s.chdel(x);
cout<<s<<endl;
}else if(command=="del"){
int a;
int b;
cin>>a;
cin>>b;
s.Delete(a,b);
cout<<s<<endl;
}else if(command=="find"){
String x;
cin>>x;
cout<<s.Find(x)<<endl;
}
}
return 0;
}