This repository was archived by the owner on Aug 17, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMyString.h
More file actions
333 lines (249 loc) · 7.5 KB
/
Copy pathMyString.h
File metadata and controls
333 lines (249 loc) · 7.5 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
#pragma once
#include<iostream>
#include<vector>
#include<string>
#include"MyChar.h"
using namespace std;
using namespace MyChar;
namespace MyString {
vector<string> SplitString(string S1, string Delim) {
vector <string> vString;
short pos;
string sWord;
while ((pos = S1.find(Delim)) != std::string::npos)
{
sWord = S1.substr(0, pos);
if (sWord != "") {
vString.push_back(sWord);
}
S1.erase(0, pos + Delim.length());
}
if (S1 != "") {
vString.push_back(S1);
}
return vString;
}
bool IsVowl(char Letter) {
Letter = tolower(Letter);
return ((Letter == 'a') || (Letter == 'e') || (Letter == 'i') || (Letter == 'o') || (Letter == 'u'));
}
void PrintFirstLetterOfEachWord(string S1) {
bool isFirstLetter = true;
cout << "\nFirst Letters of this string : \n";
for (int i = 0; i < S1.length(); i++) {
if (S1[i] != ' ' && isFirstLetter) {
cout << S1[i] << endl;
}
isFirstLetter = (S1[i] == ' ' ? true : false);
}
cout << endl;
}
void PrintEachWord(string S1) {
bool isFirstWord = true;
cout << "\nYour String Words are : \n";
for (int i = 0; i < S1.length(); i++) {
if (S1[i] != ' ' && isFirstWord) {
cout << S1[i];
if (S1[i + 1] != ' ')
continue;
}
cout << endl;
isFirstWord = (S1[i] == ' ' ? true : false);
}
}
string UpperFirstLetterOfEachWord(string S1) {
bool isFirstLetter = true;
for (int i = 0; i < S1.length(); i++) {
if (S1[i] != ' ' && isFirstLetter) {
S1[i] = toupper(S1[i]);
}
isFirstLetter = (S1[i] == ' ' ? true : false);
}
return S1;
}
string LowerFirstLetterOfEachWord(string S1) {
bool isFirstLetter = true;
for (int i = 0; i < S1.length(); i++) {
if (S1[i] != ' ' && isFirstLetter) {
S1[i] = tolower(S1[i]);
}
isFirstLetter = (S1[i] == ' ' ? true : false);
}
return S1;
}
string UpperAllString(string S1) {
for (int i = 0; i < S1.length(); i++) {
S1[i] = toupper(S1[i]);
}
return S1;
}
string LowerAllString(string S1) {
for (int i = 0; i < S1.length(); i++) {
S1[i] = tolower(S1[i]);
}
return S1;
}
string InvertAllStringLettersCase(string S1) {
for (int i = 0; i < S1.length(); i++) {
S1[i] = MyChar::InvertLetterCase(S1[i]);
}
return S1;
}
short CountCapitalLetters(string S1) {
short Counter = 0;
for (int i = 0; i < S1.length(); i++) {
//isupper(S1[i]) ? Counter++ : 0;
if (isupper(S1[i]))
Counter++;
}
return Counter;
}
short CountSmallLetters(string S1) {
short Counter = 0;
for (int i = 0; i < S1.length(); i++) {
//islower(S1[i]) ? Counter++ : 0;
if (islower(S1[i]))
Counter++;
}
return Counter;
}
short CountLetter(string S1, char Letter, bool MatchCase = true) {
short Counter = 0;
for (int i = 0; i < S1.length(); i++) {
if (MatchCase) {
if (S1[i] == Letter)
Counter++;
}
else {
if (tolower(S1[i]) == tolower(Letter))
Counter++;
}
}
return Counter;
}
short CountVowls(string S1) {
short Counter = 0;
for (int i = 0; i < S1.length(); i++) {
if (IsVowl(S1[i]))
Counter++;
}
return Counter;
}
void PrintVowlsInString(string S1) {
cout << "\nVowls in string are : ";
for (int i = 0; i < S1.length(); i++) {
if (IsVowl(S1[i]))
cout << S1[i] << " ";
}
cout << endl;
}
short CountWordsInString(string S1) {
string delim = " ";
short pos;
string sWord;
short Counter = 0;
while ((pos = S1.find(delim)) != std::string::npos)
{
sWord = S1.substr(0, pos);
if (sWord != "") {
Counter++;
}
S1.erase(0, pos + delim.length());
}
if (S1 != "") {
Counter++;
}
return Counter;
}
string TrimLeft(string S1) {
for (int i = 0; i < S1.length(); i++) {
if (S1[i] != ' ') {
// S1.erase(0, i);
//break;
return S1.substr(i, S1.length() - 1);
}
}
return "";
}
string TrimRight(string S1) {
int Length = S1.length() - 1;
for (int i = Length; i >= 0; i--) {
if (S1[i] != ' ') {
/* S1.erase(i+1, Length);
break;*/
return S1.substr(0, i + 1);
}
}
return "";
}
string Trim(string S1) {
return TrimLeft(TrimRight(S1));
}
string JoinString(vector <string>& vString, string Delim) {
string S1;
/* for (int i = 0; i < vString.size(); i++) {
if (i != vString.size()-1)
S1 += vString[i] + Delim;
else
S1 += vString[i];
}*/
for (string& Words : vString) {
S1 += Words + Delim;
}
return S1.substr(0, S1.length() - Delim.length());
}
string JoinString(string arrString[], string Delim, int Length) {
string S1 = "";
for (int i = 0; i < Length; i++) {
S1 += arrString[i] + Delim;
}
return S1.substr(0, S1.length() - Delim.length());
}
string ReverseWordsInString(string S1) {
vector <string> vString;
string S2;
vString = SplitString(S1, " ");
vector<string>::iterator iter = vString.end();
while (iter != vString.begin()) {
--iter;
S2 += *iter + " ";
}
// S2 = S2.substr(0, S2.length() - 1); //Remove the last space
S2 = TrimRight(S2);
return S2;
}
string ReplaceWordInStringUsingBuiltInFunction(string S1, string StringToReplace, string sReplaceTo) {
short pos = S1.find(StringToReplace);
while (pos != std::string::npos) {
S1 = S1.replace(pos, StringToReplace.length(), sReplaceTo);
pos = S1.find(StringToReplace);
}
return S1;
}
string ReplaceWordInStringUsingSplit(string S1, string StringToReplace, string sReplaceTo, bool MatchCase = true) {
vector <string> vString;
vString = SplitString(S1, " ");
for (string& s : vString) {
if (MatchCase) {
if (s == StringToReplace) {
s = sReplaceTo;
}
}
else {
if (LowerAllString(s) == LowerAllString(StringToReplace)) {
s = sReplaceTo;
}
}
}
return JoinString(vString, " ");
}
string RemovePunctuationsFromString(string S1) {
string S2 = "";
for (short i = 0; i < S1.length(); i++) {
if (!ispunct(S1[i])) {
S2 += S1[i];
}
}
return S2;
}
}