-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringy.cpp
More file actions
527 lines (497 loc) · 16.8 KB
/
Stringy.cpp
File metadata and controls
527 lines (497 loc) · 16.8 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
//
// Created by Christian Gould on 8/31/20.
//
# include "Stringy.h"
# include <iostream>
# include <cstring>
# include <sstream>
//# define debugg
using namespace std;
//Destructor
Stringy::~Stringy () {
delete[] ystring;
}
Stringy::Stringy () {
//initialize the string at 1 character, being the null-terminator.
ylength = 1;
ystring = new char[ylength];
ystring[ ylength - 1 ] = '\0';
}
Stringy::Stringy (const char *input) // constructor, given a c-string.
{
// A new copy of a C-string contains exactly the same number of characters in
// the C-string plus a terminating zero.
this->ylength = strlen (input) + 1;
this->ystring = new char[this->ylength];
// adds each of the elements in input including the null terminating zero.
for (int i = 0; i < this->ylength; i++) {
this->ystring[ i ] = input[ i ];
}
this->ystring[ylength-1] = '\0';
}
Stringy::Stringy (const Stringy *S) {
this->ylength = S->length ();
this->ystring = new char[ylength];
//make all the values in S part of this string
for (int i = 0; i < this->ylength; i++) {
this->ystring[ i ] = S->getString ()[ i ];
}
}
Stringy::Stringy (const Stringy &s) {
this->ylength = s.ylength;
this->ystring = new char[ylength];
strcpy (this->ystring, s.ystring);
}
Stringy::Stringy (const char *input, int size) {
this->ylength = size + 1;
char *temp = new char[ylength];
// loops only size times.
for (int i = 0; i < size; i++) {
temp[ i ] = input[ i ];
}
temp[ ylength - 1 ] = '\0';
this->ystring = temp;
}
bool Stringy::firstThree (Stringy same) {
return this->ystring[ 0 ] == same.ystring[ 0 ] &&
this->ystring[ 1 ] == same.ystring[ 1 ] &&
this->ystring[ 2 ] == same.ystring[ 2 ];
}
bool Stringy::wordInsideIt (const Stringy &word) {
return strstr (this->ystring, word.ystring) != nullptr;
}
/* Referenced Stack overflow for this function:
* https://stackoverflow.com/questions/5457608/how-to-remove-the-character-at-a-given-index-from-a-string-in-c
* */
/* Wanted to take a substring out of a larger string
* and found this article. Ended up not using it in the program. */
void Stringy::findAndDelete (char *toFind) {
// strstr returns null if needle is not found in haystack. i.e. if toFind is not found in this-> ystring.
while (strstr (this->ystring, toFind) != nullptr) {
char *begOfWord = strstr (this->ystring, toFind);
if (begOfWord == nullptr) return;
int toFindLen = strlen (toFind);
int bytesToMove = strlen (begOfWord) - toFindLen;
memmove (begOfWord, begOfWord + toFindLen,
bytesToMove);
subStrObj (0, this->length () - toFindLen-1);
this->ylength = strlen (ystring) + 1;
}
}
char & Stringy::at (int loc) const {
// this is the location without subracting 1, because of the null terminator.
if (loc < ylength) {
return ystring[ loc ];
} else {
cout << "location out of bounds in Stringy (at) function" << endl;
return ystring[ 1 ];
}
}
bool Stringy::empty () {
//check to see if the first element is null, and if the length of the cstring is only one.
return strlen (ystring) < 1;
}
int Stringy::length () const {
// returns the length with the null terminator
return (this->ylength);
}
int Stringy::charLength () const {
// returns the length without the null terminator
return strlen (this->ystring);
}
Vecty<Stringy *> *Stringy::tokenizeStringy (char delim, const Stringy &stopWords) {
Vecty<Stringy*> *returnMe = new Vecty<Stringy *> ();
char temp[10000];
Stringy *tempStringy;
stringstream ss (this->getString ());
while (ss.getline (temp, 9999, delim)) {
// avoid spaces being put into the word vector.
if (temp[ 0 ] != ' ') {
if (strstr (stopWords.ystring, temp)) continue;
tempStringy = new Stringy (temp);
returnMe->push_back (tempStringy);
} else {continue;}
}
return returnMe;
}
Vecty<Stringy *> *Stringy::tokenizeStringy (char delim, const Stringy &stopWords, int size) {
auto *returnMe = new Vecty<Stringy *> ();
char temp[10000];
Stringy *tempStringy;
stringstream ss (this->getString ());
while (ss.getline (temp, 9999, delim)) {
// avoid spaces being put into the word vector.
if (temp[ 0 ] != ' ') {
if (strstr (stopWords.ystring, temp)) continue;
tempStringy = new Stringy (temp, size);
returnMe->push_back (tempStringy);
} else {continue;}
}
return returnMe;
}
void Stringy::subStrObj (int index, int length) {
int size = length + 1;
char *temp = new char[size];
//double for loop, in which it will end at a specified length "i" in the string,
//and starts at zero for the new temp string.
for (int i = index, j = 0; j < size; i++, j++) {
temp[ j ] = ystring[ i ];
}
//make sure the end of the c-string is null-terminated.
if (temp[ size - 1 ] != '\0') {temp[ size - 1 ] = '\0';}
delete this->ystring;
this->ystring = temp;
this->ylength = size;
}
Stringy Stringy::substr (int index, int length) {
int size = length + 1;
char *temp = new char[size];
//double for loop, in which it will end at a specified length "i" in the string,
//and starts at zero for the new temp string.
for (int i = index, j = 0; j < size; i++, j++) {
temp[ j ] = ystring[ i ];
}
//make sure the end of the c-string is null-terminated.
if (temp[ size - 1 ] != '\0') {temp[ size - 1 ] = '\0';}
return Stringy (temp);
}
// Function to set the string to a new string.
void Stringy::setString (const char *input) {
// get the new length
this->ylength = strlen (input) + 1;
// copy over the new data
this->ystring = new char[ylength];
for (int i = 0; i < ylength - 1; i++) {
this->ystring[ i ] = input[ i ];
}
// add the null terminator
this->ystring[ ylength - 1 ] = '\0';
}
char *Stringy::getString () const {
return this->ystring;
}
int Stringy::find_Number_Inside (Stringy *toFind) {
int returnMe = 0;
//cout << "Stringy toFind: " << *toFind << endl;
//cout << "char* findMe: " << toFind-> getString () << endl;
char *findMe = toFind->getString ();
while (strstr (this->ystring, findMe) != nullptr && (strcmp (strstr (this->ystring, findMe), " ")) != 0 &&
strcmp (findMe, "") != 0) {
int toFindLen = strlen (findMe);
int bytesToMove = strlen (strstr (this->ystring, findMe)) - toFindLen;
returnMe++;
/* this will find where the toFind string is inside of the ystring, then delete the word that was found
by moving the data over the number of places that the length of toFind is. */
// cout << "toFind: " << toFind << endl;
// cout << "Destination: " << strstr(this->ystring,findMe) << endl;
// cout << "Source: " << strstr(this->ystring, findMe) + strlen(findMe) << endl;
// cout << "Number of Bytes: " << bytesToMove << endl;
// cout << "******************" << endl;
memmove (strstr (this->ystring, findMe), strstr (this->ystring, findMe) + strlen (findMe),
bytesToMove);
subStrObj (0, this->length () - toFindLen);
this->ylength = strlen (ystring) + 1;
}
return returnMe;
}
void Stringy::clean () {
// for every character in the string
for (int i = 0; i < ylength - 1; i++) {
// if the character is not a letter
if (!(((int) ystring[ i ] > 64 && (int) ystring[ i ] < 90) ||
((int) ystring[ i ] > 96 && (int) ystring[ i ] < 123))) {
// replace it with a space.
//cout << (int) ystring[i] << " = " << ystring[i] << endl;
//cout << ystring[i-1] << ystring[i] << ystring[i+1] << " becomes ";
ystring[ i ] = ' ';
//cout << ystring[i-1] << ystring[i] << ystring [i+1] << endl;
}
}
}
bool Stringy::findSentiment (Stringy total) {
// whole function will not execute if Stringy is empty.
if (total.empty ()) return false;
bool returnMe = false;
for (int i = 0; i < total.length (); i++) {
if (i == total.length () - 1) {return false;}
else {
// will only return true if ",p or ",|| find->getString ()[i] == '\t' || find-> getString ()[i] == '|')|| find->getString ()[i] == '\t' || find-> getString ()[i] == '|')
if (total.getString ()[ i ] == '"' &&
(total.getString ()[ i + 1 ] == ',' || total.getString ()[ i + 1 ] == '\t' ||
total.getString ()[ i + 1 ] == '|') && total.getString ()[ i + 2 ] == 'p') {
for (int x = i; x < 8; x++) {
total.getString ()[ i ] = '\0';
}
return true;
} else if (total.getString ()[ i ] == '"' &&
(total.getString ()[ i + 1 ] == ',' || total.getString ()[ i + 1 ] == '\t' ||
total.getString ()[ i + 1 ] == '|') && total.getString ()[ i + 2 ] == 'n') {
for (int x = i; x < 8; x++) {
total.getString ()[ i ] = '\0';
}
return false;
}
}
}
return returnMe;
}
ostream &operator<< (ostream &OS, const Stringy &S) {
OS << S.ystring;
return OS;
}
istream &operator>> (istream &IS, Stringy &S) {
// get data into c string
char tempArr[1000];
IS >> tempArr;
// initialize the length of the string
S.ylength = strlen (tempArr) + 1;
// copy over the data
for (int i = 0; i < S.ylength - 1; i++) {
S.ystring[ i ] = tempArr[ i ];
}
// add null terminator
S.ystring[ S.ylength - 1 ] = '\0';
return IS;
}
Stringy &Stringy::operator= (const Stringy &S) {
// if it is self assignment, return this Stringy
if (this == &S) return *this;
//set the length the same, then delete this Stringy's string.
this->ylength = S.ylength;
delete[] this->ystring;
this->ystring = new char[this->ylength];
// copy each of the elements of S to this Stringy.
for (int i = 0; i < (this->ylength); i++) {
this->ystring[ i ] = S.ystring[ i ];
}
// return this Stringy.
return *this;
}
bool operator== (const Stringy &S1, const Stringy &S2) {
return strcmp (S1.getString (), S2.getString ()) == 0;
}
Stringy &operator+ (const Stringy &S1, const Stringy &S2) {
//add the lengths minus the null terminator, then add one to rep. the new null terminator.
int length = (S1.ylength - 1) + (S2.ylength - 1) + 1;
// initialize the array that will be used
char *array = new char[length];
// add the first array to the full array, without the null terminator.
for (int i = 0; i < S1.ylength - 1; i++) {
array[ i ] = S1.ystring[ i ];
}
// add the second array to the full array, without the null terminator.
for (int i = 0; i < S2.ylength - 1; i++) {
array[ i + (S1.ylength - 1) ] = S2.ystring[ i ];
}
// add the null terminator to the end of the new array.
array[ length - 1 ] = '\0';
// create the new Stringy object which will store this new added c-String.
auto *returnMe = new Stringy (array);
// delete the temp variable
delete[] array;
// return the new Stringy
return *returnMe;
}
Stringy &operator+ (const Stringy &S1, const char *addition) {
char *newStringy = nullptr;
int newlen = strlen (S1.getString ()) + strlen (addition) + 1;
newStringy = new char[newlen];
for (int i = 0; i < strlen (S1.getString ()); i++) {
newStringy[ i ] = S1.getString ()[ i ];
}
for (int i = strlen (S1.getString ()), j = 0; i < newlen; i++, j++) {
newStringy[ i ] = addition[ j ];
}
newStringy[ newlen ] = '\0';
return *new Stringy (newStringy);
}
/* reference:
* https://www.cplusplus.com/forum/beginner/50062/
* */
Stringy &operator+ (const Stringy &S1, const int num_Toadd) {
int counter = 0;
int number = num_Toadd;
// counter will be how many digits inside the number.
while (number) {
number = number / 10;
counter++;
}
int returnLen = S1.length () + counter;
char *putMeInStringy = new char[returnLen];
// put the elements of S1 into the new char
for (int i = 0; i < S1.length () - 1; i++) {
putMeInStringy[ i ] = S1.getString ()[ i ];
}
char numberToAddToString[counter + 1];
stringstream strs;
strs << num_Toadd;
strs.getline (numberToAddToString, counter + 1);
for (int i = S1.length () - 1, j = 0; i < returnLen - 1; i++, j++) {
putMeInStringy[ i ] = numberToAddToString[ j ];
}
putMeInStringy[ returnLen - 1 ] = '\0';
return *new Stringy (putMeInStringy);
}
Stringy &Stringy::operator+= (const Stringy &S1) {
// make total length equal to the original length minus null plus new stringy minus null plus new null
int newLength = (this->ylength - 1) + (S1.ylength - 1) + 1;
int oldlength_WO_Null = this->ylength - 1;
// save the old string in its original form without null.
char oldString[ylength];
for (int i = 0; i < ylength - 1; i++) {
oldString[ i ] = ystring[ i ];
}
// reset the string in this object.
this->ylength = newLength;
delete[] this->ystring;
this->ystring = new char[ylength];
// add the two strings together into a new Stringy, excluding the null terminator
for (int i = 0; i < oldlength_WO_Null; i++) {
this->ystring[ i ] = oldString[ i ];
}
// add the second string to the new string, including the null terminator
for (int i = oldlength_WO_Null, j = 0; i < ylength; i++, j++) {
this->ystring[ i ] = S1.ystring[ j ];
}
// return the Stringy.
return *this;
}
bool operator< (const Stringy &lhs, const Stringy &rhs) {
return strcmp (lhs.ystring, rhs.ystring) < 0;
}
void Stringy::toUpper () {
// for each of the letters in the string
for(int i = 0; i < this-> ylength; i++){
if ((int)ystring[i] <= 122 && (int)ystring[i] >= 97){
ystring[i] = (int)ystring[i] - 32;
}
}
}
Vecty<pair<Stringy, Stringy *>> Stringy::check_For_Goups () {
Vecty<pair<Stringy,Stringy*>> returnMe;
char spaceSeparated[80];
Stringy word_Or_Phrase_to_Index;
Stringy* group_Name = nullptr;
stringstream ss(this->ystring);
// this will get each of the pieces space-separated
while(ss.getline(spaceSeparated, 79, ' ')){
#ifdef debugg
cout << "Top of while loop, spaceSeparated: " << spaceSeparated << endl;
#endif
// if the first word is a phrase
if (spaceSeparated[0] == '[') {
word_Or_Phrase_to_Index = this->getPhrase(ss, spaceSeparated);
//cout << "The word or phrase this round is: " << word_Or_Phrase_to_Index << endl;
}
// if the first word is not a phrase
else if (spaceSeparated[0] != '['){
word_Or_Phrase_to_Index = this->getWord(spaceSeparated);
}
// check if there is a group
if(ss.peek() == '('){
ss.getline(spaceSeparated, 79, ' ');
}
char* parentheses = strstr(spaceSeparated, "(");
// if there is a group
if (parentheses != nullptr){
#ifdef debugg
cout << "There is a group, parentheses: " << parentheses << "expected '(' "<< endl;
#endif
// if the group is a phrase
//cout << "parentheses of 1: " << parentheses[1] << endl;
if (parentheses[1] == '['){
parentheses = parentheses + 1;
group_Name = new Stringy(this->getPhrase (ss,parentheses));
ss.get();
if(ss.peek() == ' ') ss.get();
}
// if the group is a word
else {
// start one after the parenthetical, at the word itself
char buffer[79];
stringstream tempStream(parentheses+1);
tempStream.getline(buffer,78,')');
//cout << "buffer ends up being: " << buffer << endl;
group_Name = new Stringy(buffer);
}
}
// if there is no group
else {
group_Name = nullptr;
}
// push back the pair of the word/phrase and the group name into the return variable
returnMe.push_back (pair<Stringy, Stringy*>(word_Or_Phrase_to_Index,group_Name));
memset(spaceSeparated, '\0', sizeof(spaceSeparated));
} // end while getline
return returnMe;
}
Stringy Stringy :: getPhrase(stringstream& ss, char spaceSeparated[]){
Stringy word_Or_Phrase_to_Index = "";
word_Or_Phrase_to_Index = word_Or_Phrase_to_Index + (spaceSeparated+1);
word_Or_Phrase_to_Index = word_Or_Phrase_to_Index + " ";
ss.getline(spaceSeparated, 79, ']');
#ifdef debugg
cout << "Got: " << spaceSeparated << endl;
#endif
word_Or_Phrase_to_Index = word_Or_Phrase_to_Index + spaceSeparated;
memset(spaceSeparated, '\0', sizeof(spaceSeparated));
#ifdef debugg
cout << "returning: " << word_Or_Phrase_to_Index << endl;
#endif
if(ss.peek() == ' ')ss.get();
return word_Or_Phrase_to_Index;
}
Stringy Stringy :: getWord(char spaceSeparated[]){
char* foundParenthetical = strstr(spaceSeparated, "(");
// if there is no group, just return the stringy of the given char array
if(foundParenthetical == nullptr){
return Stringy(spaceSeparated);
// if there is a group, ignore the group using a stringstream
} else {
stringstream tempStream(spaceSeparated);
char buffer[79];
tempStream.getline(buffer, 78, '(');
return Stringy(buffer);
}
}
Stringy Stringy::get_Pg_Nmbr () {
char returnChar[100];
stringstream ss(this->ystring);
// get rid of the beginning < character
ss.get();
// pull the number
ss.getline(returnChar,100, '>');
return Stringy(returnChar);
}
void Stringy::remPunct () {
int counter = this->ylength - 2;
while (counter > 0 && !isNtPunct(ystring[counter]) ){
this->subStrObj (0,counter);
counter--;
}
}
bool Stringy:: isNtPunct(char isIt){
int ascii = (int) isIt;
return ((ascii > 64 && ascii < 91) || (ascii == 40) || (ascii == 41) || (ascii == 91) || (ascii == 93));
}
bool operator<= (const Stringy &lhs, const Stringy& rhs) {
return ((lhs < rhs) || lhs == rhs);
}
int Stringy::toInt () {
return stoi(this->ystring);
}
bool operator!= (const Stringy &S1, const Stringy& S2) {
return strcmp(S1.ystring, S2.ystring) != 0;
}
Stringy::Stringy (const int& input) {
stringstream strm;
strm << input;
char num[50];
strm.getline(num,49);
ylength = strlen (num) + 1;
this-> ystring = new char[ylength];
for(int i = 0; i < strlen(num); i++){
this->ystring[i] = num[i];
}
this-> ystring[ylength -1] = '\0';
}