-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringBuilder.cpp
253 lines (234 loc) · 6.1 KB
/
StringBuilder.cpp
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
#include <bits/stdc++.h>
using namespace std;
//Linked list node structure
struct node
{
const char *str; // store pointer to string
struct node *next; // store pointer to node
};
//create new node by allocating memory
struct node *getnode(const char *s)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->str = s;
newnode->next = NULL;
}
//class StringBuilder which store total string length and head pointer of all string node
class stringBuilder
{
public:
int len;
struct node *head;
//Default constructor
stringBuilder()
{
len = 0;
head = NULL;
}
//Parameterised contructor
stringBuilder(const char *s)
{
len = strlen(s);
head = getnode(s);
}
};
//Intialised stringBuilder object with given string
stringBuilder stringInitialize(const char *str)
{
stringBuilder sobj(str);
return sobj;
}
//print all node of stringBuilder which append to it.
void printall(stringBuilder ss)
{
struct node *tt = ss.head;
while (tt)
{
printf("%s-", tt->str);
tt = tt->next;
}
cout<<" :::: length : "<<ss.len<<endl;
}
//append string s2 to s1 and returns new object
stringBuilder stringAppend(stringBuilder s1, stringBuilder s2)
{
if (s1.head == NULL & s2.head == NULL)
{
return stringBuilder();
}
stringBuilder s3;
struct node *cur, *pre, *newhead = NULL;
//append all string node from s1
if (s1.head != NULL)
{
cur = s1.head;
pre = (struct node *)malloc(sizeof(struct node));
pre->str = s1.head->str;
pre->next = NULL;
newhead = pre;
cur = cur->next;
while (cur != NULL)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->str = cur->str;
new_node->next = NULL;
pre->next = new_node;
pre = new_node;
cur = cur->next;
}
}
//append all string node from s2
if(s2.head != NULL)
{
cur = s2.head;
struct node *firofs2 = (struct node *)malloc(sizeof(struct node));
firofs2->str = cur->str;
firofs2->next = NULL;
if(s1.head==NULL)
{
pre=firofs2;
newhead=pre;
}
else{
pre->next = firofs2;
pre = firofs2;
}
cur = cur->next;
while (cur != NULL)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->str = cur->str;
new_node->next = NULL;
pre->next = new_node;
pre = new_node;
cur = cur->next;
}
}
s3.head = newhead;
s3.len = s1.len + s2.len;
return s3;
}
//merge multiple string node of s1 to single string node
void mergeallnodestring(stringBuilder &s1)
{
struct node *pre;
struct node *cur=s1.head;
char *finalstr= new char[s1.len];
int finallen=s1.len;
while(cur!=NULL)
{
strcat(finalstr,cur->str);
pre=cur;
cur=cur->next;
free(pre);
}
struct node *new_node = (struct node *)malloc(sizeof(struct node));
//printf("\nfinal string : %s",finalstr);
//cout<<endl;
new_node->str=finalstr;
new_node->next=NULL;
s1.head=new_node;
s1.len=finallen;
}
void preKMPcompute(const char *sub, int patlen, int *kmpNext)
{
int len = 0;
kmpNext[0] = 0;
int i = 1;
while (i < patlen)
{
if (sub[i] == sub[len]) {
len++;
kmpNext[i] = len;
i++;
}
else
{
if(len != 0)
len = kmpNext[len - 1];
else
{
kmpNext[i] = 0;
i++;
}
}
}
}
// find substr from source string s1 and return index founded
// if substr not found then return -1
int findSubstring(stringBuilder &s1,const char *substr)
{
if(s1.head==NULL)
{
cout<<"Your Source string is empty !!!"<<endl;
return -1;
}
else if(strlen(substr)==0)
{
cout<<"Your Pattern is empty !!!"<<endl;
return -1;
}
else
{
if((s1.head)->next!=NULL)
mergeallnodestring(s1);
//printall(s1);
const char *sourcestr=(s1.head)->str;
int patlen = strlen(substr);
int sourcelen = strlen(sourcestr);
int i = 0; // to keep track of sourcestring
int j = 0; // to keep track of substr
int kmpNext[patlen];
preKMPcompute(substr, patlen, kmpNext);
while (i < sourcelen)
{
//when sourcestr and substr match incremeent both index i,j
if (substr[j] == sourcestr[i])
{
j++;
i++;
}
// when j reached to its length means substr found and return
if (j == patlen)
{
return (i-j);
}
//when sourcestr[i] and substr[j] doesn't match
else if (i < sourcelen && substr[j] != sourcestr[i])
{
//if J is not first index(0) then assigned previous index kmpNext
if (j != 0)
j = kmpNext[j - 1];
// if j is 0 then increment i by 1
else
i = i + 1;
}
}
// if pattern not found in soucestr then return -1
if(i==sourcelen)
return -1;
}
}
int main()
{
stringBuilder s1 = stringInitialize("hello");
stringBuilder s2 = stringInitialize("World");
stringBuilder s4 = stringInitialize("dofeshjbjvs");
stringBuilder s5 = stringInitialize("IamDk");
stringBuilder s3=stringAppend(s1,s2);
printall(s3);
s1=stringAppend(s3,s4);
s1=stringAppend(s1,s5);
printall(s3);
printall(s1);
int ind;
if((ind=findSubstring(s1,"of")) != -1)
{
cout<<"\nPattern found at index : "<<ind<<endl;
}
else
{
cout<<"\nPattern not Found !!!"<<endl;
}
//printall(s3);
}