-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 14.2 (Completed)
419 lines (345 loc) · 9.14 KB
/
Exercise 14.2 (Completed)
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
1. (Practice) Enter and run Program 14.8.
//Done!!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str = "This - 123 / is 567 A ? <6245> Test!";
char nextChar;
int i;
int numLetters = 0, numDigits = 0, numOthers = 0;
cout << "The original string is : " << str
<< "\nThis string contains " << int(str.length())
<< " characters, " << " which consist of" << endl;
// check each character in the string
for (i = 0; i < int(str.length()); i++)
{
nextChar = str.at(i); // get a character
if (isalpha(nextChar))
numLetters++;
else if (isdigit(nextChar))
numDigits++;
else
numOthers++;
}
cout << " " << numLetters << " letters" << endl;
cout << " " << numDigits << " digits" << endl;
cout << " " << numOthers << " other characters." << endl;
cin.ignore();
system("PAUSE");
return 0;
}
2. (Practice) Enter and run Program 14.9.
//Done!!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string str;
cout << "Type in any sequence of characters : ";
getline(cin, str);
// cycle through all elements of the string
for (i = 0; i < int(str.length()); i++)
str[i] = toupper(str[i]);
cout << "The characters just entered, in uppercase, are: "
<< str << endl;
system("PAUSE");
return 0;
}
3. (Program) Write a C++ program that counts the number of words in a string. A word is
encountered whenever a transition from a blank space to a nonblank character is encountered.
The string contains only words separated by blank spaces.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string str;
int words = 0;
cout << "Type in any sequence of characters : ";
getline(cin, str);
// cycle through all elements of the string
for (i = 0; i < int(str.length()); i++){
if (str.at(i) == ' '){
words++;
}
}
cout << "The number of words are " << words << endl;
system("PAUSE");
return 0;
}
4. (Practice) Generate 10 random numbers in the range 0 to 129. (If necessary, review Section 6.8
for how to do this.) If the number represents a printable character, print the character with a
message that indicates the following:
The character is a lowercase letter.
The character is an uppercase letter.
The character is a digit.
The character is a space.
If the character is none of these, display its value in integer format.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i,j;
string str;
// cycle through all elements of the string
for (i = 0; i < 10; i++){
j = rand() % 129;
str = (char)(j);
if (j >= 65 && j <= 90){
cout << "The character is an uppercase letter. " << endl;
cout << str << endl;
}
else if (j >= 97 && j <= 122){
cout << "The character is a lowercase letter. " << endl;
cout << str << endl;
}
else if (j >= 48 && j <= 57){
cout << "The character is a digit. " << endl;
cout << str << endl;
}
else if (j == 32){
cout << "The character is a space" << endl;
cout << str << endl;
}
else {
cout << "The character is none of these" << endl;
cout << j << endl;
}
}
system("PAUSE");
return 0;
}
5. (Practice) a. Write a function named reverse() that returns a string in reverse order without
using the string class’s length() method.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void reverse(string str) {
reverse(str.begin(), str.end());
cout << str << endl;
}
b. Write a simple main() function to test the reverse() function written for Exercise 5a.
int main()
{
reverse("Awesome");
system("PAUSE");
return 0;
}
6. (Practice) a. Write a function named countlets() that returns the number of letters in a
string passed as an argument. Digits, spaces, punctuation, tabs, and newline characters
shouldn’t be included in the returned count.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void countlets(string str) {
int count = 0;
for (int i = 0; i < str.length();i++)
if (str.at(i) <= 'z' && str.at(i) >= 'a'){
count++;
}
cout << count << endl;
}
b. Include the countlets() function written for Exercise 6a in an executable C++ program,
and use the program to test the function.
int main()
{
countlets("cool it worked");
system("PAUSE");
return 0;
}
7. (Program) Write a program that accepts a string from the console and displays the hexadecimal
equivalent of each character in the string.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
cin >> str;
for (int i = 0; i < str.length(); i++)
cout << hex << (str.at(i) - '0') << endl;
system("PAUSE");
return 0;
}
8. (Program) Write a C++ program that accepts a string from the keyboard and displays the
string one word per line.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
getline(cin, str);
for (int i = 0; i < str.length(); i++){
if (str.at(i) == ' '){
str.at(i) = '\n';
}
}
cout << str << endl;
system("PAUSE");
return 0;
}
9. (Debug) In response to the following code, suppose a user enters the data 12e4:
cout << “Enter an integer: “;
cin >> value;
What value will be stored in the integer variable value?
12 will be stored in the integer variable value.
10. (Useful utility) a. Write a C++ function that accepts a string and two character values. The
function should return the string with each occurrence of the first character replaced by the
second character.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void replace(string str, char one, char two){
for (int i = 0; i < str.length(); i++){
if (str.at(i) == one){
str.at(i) = two;
}
}
cout << str << endl;
}
int main()
{
replace("Patrick", 'P', 'k');
system("PAUSE");
return 0;
}
b. Test the function written for Exercise 10a by writing a program that accepts a string from
the user, calls the function written for Exercise 10a to replace all occurrences of the letter e
with the letter x from the user-entered string, and then displays the changed string.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void replace( char one, char two){
string str;
cout << "Enter your string " << endl;
getline(cin, str);
for (int i = 0; i < str.length(); i++){
if (str.at(i) == one){
str.at(i) = two;
}
}
cout << str << endl;
}
int main()
{
replace( 'e', 'x');
system("PAUSE");
return 0;
}
11. (Useful utility) Modify the function written for Exercise 10a to search for all occurrences
of a user-entered sequence of characters, and then replace this sequence, when it’s found in
the string, with a second user-entered sequence. For example, if the entered string is
Figure 4-4 illustrates the output of Program 4-2 and the user specifies that 4-
is to be replaced by 3-, the resulting string is Figure 3-4 illustrates the output of
Program 3-2. (All occurrences of the searched-for sequence have been changed.)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void replace(){
string str;
string one, two;
cout << "Enter your string " << endl;
getline(cin, str);
getline(cin, one);
getline(cin, two);
while (str.find(one) != std::string::npos){
str.replace(str.find(one), one.length(), two);
}
cout << str << endl;
}
int main()
{
replace();
system("PAUSE");
return 0;
}
12. (Program) a. Write a C++ program that stops reading a line of text when a period is entered and
displays the sentence with correct spacing and capitalization. For this program, correct spacing
means only one space should be used between words, and all letters should be lowercase, except
the first letter. For example, if the user enters the text i am going to Go TO
THe moVies., the displayed sentence should be I am going to go to the movies.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void removeSpaces(string &str)
{
int n = str.length();
int i = 0, j = -1;
bool spaceFound = false;
while (++j < n && str[j] == ' ');
while (j < n)
{
if (str[j] != ' ')
{
if ((str[j] == '.' || str[j] == ',' ||
str[j] == '?') && i - 1 >= 0 &&
str[i - 1] == ' ')
str[i - 1] = str[j++];
else
str[i++] = str[j++];
spaceFound = false;
}
else if (str[j++] == ' ')
{
if (!spaceFound)
{
str[i++] = ' ';
spaceFound = true;
}
}
}
if (i <= 1)
str.erase(str.begin() + i, str.end());
else
str.erase(str.begin() + i - 1, str.end());
}
int main()
{
string str = "i am going to Go TO THe moVies. ";
removeSpaces(str);
for(int i = 0; i < str.length(); i++){
if (str[i] == str[0]){
str[i] = toupper(str[i]);
}
if (str[i] != str[0]){
str[i] = tolower(str[i]);
}
if (str[i] == '.'){
break;
}
}
cout << str << endl;
system("PAUSE");
return 0;
}
b. Determine what characters, if any, aren’t displayed correctly by the program you created for
Exercise 12a.
None, program works properly!!