-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringsBook
More file actions
321 lines (279 loc) · 8.07 KB
/
StringsBook
File metadata and controls
321 lines (279 loc) · 8.07 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
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int strLength(char* s1);
void prtStringBackwards(char s1[], int length); //#2 accepts c-string, prints string backwards
void replaceSubstring(char str1[], char str2[], char str3[], int length);
void replaceSubstring(string str1, string str2, string str3); //overloaded function
void getPassword(char pw[]); //#5. enter password to be verified
bool verifyLength(char pw[]); //#5 verify password bu checking length
bool verifyCase(const char* pw); // at least one upper and lowercase letter
bool verifyDigit(const char* pw); //at least one digit
void prtPasswordVerification(char pw[], bool l, bool c, bool d);
string getPassword(); //#5. enter password to be verified
bool verifyLength(string pw); //#5 verify password bu checking length
bool verifyCase(string pw); // at least one upper and lowercase letter
bool verifyDigit(string pw); //at least one digit
void prtPasswordVerification(string pw, bool l, bool c, bool d);
void wordSeparator(char strA[], char strB[]);
int main()
{
char s1[] = "This is a test string for string length";
int length = 0;
length = strLength(s1);
cout << "1. String Length: \"This is a test string for string length\" has "
<< length << " characters" << endl;
char* ptr;
ptr = s1;
cout << "2. Print \"This is a test string for string length\" backwards: ";
prtStringBackwards(ptr, length);
cout << endl;
char str1[] = "the dog jumped over the fence";
char str2[] = "the";
char str3[] = "that";
length = strLength(str3);
replaceSubstring(str1, str2, str3, length);
cout << "3. Replace \"the\" with \"that\" in \"the dog jumped over the fence\":" << endl;
for(int i = 0; i < strLength(str1); i++)
cout << str1[i];
cout << endl;
char Str1[] = "Sid likes to eat nuts but the brother, Sidney likes to eat fruit. One time Sid put a pecan in Sidney's banana and boy hidey did Sid get a chuckle out of Sidney's misfortune";
char Str2[] = "Sid";
char Str3[] = "Mikey";
length = strLength(Str3);
replaceSubstring(Str1, Str2, Str3, length);
cout << "3. Replace \"Sid\" with \"Mikey\" in \"Sid likes to eat nuts but the brother, " << endl;
cout << "Sidney likes to eat fruit. One time Sid put a pecan in Sidney's banana and boy " << endl;
cout << "hidey did Sid get a chuckle out of Sidney's misfortune\":" << endl;
for(int i = 0; i < strLength(Str1); i++)
cout << Str1[i];
cout << endl;
string classstr1, classstr2, classstr3;
classstr1 = "the dog jumped over the fence";
classstr2 = "the";
classstr3 = "that";
replaceSubstring(classstr1, classstr2, classstr3);
cout << "4. " << classstr1;
string Classstr1, Classstr2, Classstr3;
Classstr1 = "Sid likes to eat nuts but the brother, Sidney likes to eat fruit. One time Sid put a pecan in Sidney's banana and boy hidey did Sid get a chuckle out of Sidney's misfortune";
Classstr2 = "Sid ";
Classstr3 = "Mikey ";
replaceSubstring(Classstr1, Classstr2, Classstr3);
cout << "4. " << Classstr1 << endl;
bool l, c, d; //results of verification length, case, and digit functions
char pw[20];
//char* ptr = NULL;
ptr = pw;
//int *ptr = NULL;
bool again = true;
while(again)
{
getPassword(pw);
l = verifyLength(pw);
c = verifyCase(ptr);
d = verifyDigit(ptr);
prtPasswordVerification(pw, l, c, d);
cout << "Create another password? Enter 1 for YES, 0 for NO: ";
cin >> again;
}
string classpw;
again = true;
while(again)
{
classpw = getPassword();
l = verifyLength(classpw);
c = verifyCase(classpw);
d = verifyDigit(classpw);
prtPasswordVerification(classpw, l, c, d);
cout << "Create another password? Enter 1 for YES, 0 for NO: ";
cin >> again;
}
char strA[200] = "SidLikesToEatNutsButTheBrother,SidneyLikesToEatFruit.OneTimeSidPutAPecanInSidney'sBananaAndBoyHideyDidSidGetAChuckleOutOfSidney'sMisfortune";
char strB[200];
wordSeparator(strA, strB);
int i = 0;
strlwr(strB);
cout << "7. ";
while (strB[i] != 0)
{
if(strB[i] == '.')
strB[i+2] = toupper(strB[i+2]);
cout << strB[i];
i++;
}
return 0;
}
void prtStringBackwards(char s1[], int length)
{
for (length; length >= 0; length--)
{
cout << s1[length];
}
return;
}
int strLength(char* s1)
{
int length = 0;
while(*s1 != '\0')
{
s1++;
length++;
}
return length;
}
void replaceSubstring(char str1[], char str2[], char str3[], int length)
{
char* subptr = NULL; //pointer to occurence of str2 in str1
subptr = strstr(str1, str2);
while(subptr != NULL)
{
for(int i = 0; i < length; i++)
{
*subptr = str3[i];
subptr++;
}
subptr = strstr(str1, str2);
}
return;
}
void replaceSubstring(string str1, string str2, string str3)
{
int position = 0;
int length = 0;
position = str1.find(str2);
length = str2.length();
while (position != -1) //if substring is not found, npos is returned with a value of -1
{
str1.replace(position,length, str3);
position = str1.find(str2);
}
return;
}
void getPassword(char pw[])
{
// int* ptr;
// ptr = pw;
// char pw[] = "";
cout << "5. Create a new password: ";
cin.ignore(20, '\n');
cin.getline(pw, 20);
//for(int i = 0; i < 20; i++)
// cout << pw[i]; //for debug to make sure password read in correctly
return;
}
bool verifyLength(char pw[])
{
if(strLength(pw) >= 7)
return true;
else
return false;
}
bool verifyCase(const char* ptr)
{
int countUpper = 0, countLower = 0;
const char *endptr = ptr;
endptr += 20;
for(ptr; ptr < endptr; ptr++)
{
if(isupper(*ptr))
countUpper++;
if(islower(*ptr))
countLower++;
}
if(countUpper >= 1 && countLower >= 1)
return true;
else
return false;
}
bool verifyDigit(const char* ptr)
{
int countDigit = 0;
const char* endptr = ptr;
endptr += 20;
for(ptr; ptr < endptr; ptr++)
{
if(isdigit(*ptr))
countDigit++;
}
if(countDigit >= 1)
return true;
else
return false;
}
void prtPasswordVerification(char pw[], bool l, bool c, bool d)
{
if (!l)
cout << "Password must be at least seven characters long." << endl;
if(!c)
cout << "Password must contain at least one upper and one lower case letter." << endl;
if(!d)
cout << "Password must contain at least one digit." << endl;
else if(l == true && c == true && d == true)
cout << "Password created." << endl;
return;
}
string getPassword()
{
string pw;
cout << "6. Create a new password: ";
cin >> pw;
return pw;
}
bool verifyLength(string pw)
{
if(pw.length() >= 7)
return true;
else
return false;
}
bool verifyCase(string pw)
{
const char* ptr;
ptr = pw.c_str();
if(verifyCase(ptr))
return true;
else
return false;
}
bool verifyDigit(string pw)
{
const char* ptr;
ptr = pw.c_str();
if(verifyDigit(ptr))
return true;
else
return false;
}
void prtPasswordVerification(string pw, bool l, bool c, bool d)
{
if (!l)
cout << "Password must be at least seven characters long." << endl;
if(!c)
cout << "Password must contain at least one upper and one lower case letter." << endl;
if(!d)
cout << "Password must contain at least one digit." << endl;
else if(l == true && c == true && d == true)
cout << "Password created." << endl;
return;
}
void wordSeparator(char strA[], char strB[])
{
strB[0] = strA[0];
int j = 1;
for(int i = 0; i < 200; i++)
{
if(islower(strA[i]))
{
strB[j] = strA[i];
j++;
}
else
{
strB[j] = ' ';
strB[j+1] = strA[i];
j+=2;
}
}
return;
}