-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 14.1 (Completed)
272 lines (240 loc) · 6.11 KB
/
Exercise 14.1 (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
1. (Practice) Enter and run Program 14.2.
//Done!!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message; // declare a string object
cout << "Enter a string : \n";
getline(cin, message);
cout << "The string just entered is : \n"
<< message << endl;
system("PAUSE");
return 0;
}
2. (Practice) Determine the value of text.at(0), text.at(3), and text.at(10), assuming
for each one that text is each of the following strings:
a. Now is the time
text.at(0) = N
text.at(3) = (space)
text.at(10) = t
b. Rocky raccoon welcomes you
text.at(0) = R
text.at(3) = k
text.at(10) = o
c. Happy Holidays
text.at(0) = H
text.at(3) = p
text.at(10) = a
d. The good ship
text.at(0) = T
text.at(3) = (space)
text.at(10) = i
3. (Practice) Enter and run Program 14.5.
//Done!!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Counting the number of vowels";
int i, numChars;
int vowelCount = 0;
cout << "The string : " << str << endl;
numChars = int(str.length());
for (i = 0; i < numChars; i++)
{
switch (str.at(i)) // here's where a character is retrieved
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelCount++;
}
}
cout << "has " << vowelCount << " vowels." << endl;
system("PAUSE");
return 0;
}
4. (Modify) Modify Program 14.5 to count and display the numbers of each vowel contained in
the string.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Counting the number of vowels";
int i, numChars;
int vowelCount5 = 0;
int vowelCount1 = 0;
int vowelCount2 = 0;
int vowelCount3 = 0;
int vowelCount4 = 0;
cout << "The string : " << str << endl;
numChars = int(str.length());
for (i = 0; i < numChars; i++)
{
switch (str.at(i)) // here's where a character is retrieved
{
case 'a':
vowelCount1++;
break;
case 'e':
vowelCount2++;
break;
case 'i':
vowelCount3++;
break;
case 'o':
vowelCount4++;
break;
case 'u':
vowelCount5++;
break;
}
}
cout << "has " << vowelCount1 << " A vowels" << endl;
cout << vowelCount2 << " E vowels" << endl;
cout << vowelCount3 << " I vowels" << endl;
cout << vowelCount4 << " O vowels" << endl;
cout << vowelCount5 << " U vowels" << endl;
system("PAUSE");
return 0;
}
5. (Modify) Modify Program 14.5 to display the number of vowels in a user-entered string.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int i, numChars;
int vowelCount = 0;
cout << "Enter your String " << endl;
cin >> str;
cout << "The string : " << str << endl;
numChars = int(str.length());
for (i = 0; i < numChars; i++)
{
switch (str.at(i)) // here's where a character is retrieved
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelCount++;
}
}
cout << "has " << vowelCount << " vowels." << endl;
system("PAUSE");
return 0;
}
6. (Program) Using the at() method, write a C++ program that reads in a string by using
getline(), stores the string in a string object named message, and then displays the
string in reverse order. (Hint: After the string has been entered and saved, retrieve and display
characters, starting from the end of the string. The last character is located at the position
message.length() - 1.)
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message;
getline(cin, message);
for (int i = message.length()-1; i < message.length(); --i){
cout << message.at(i)<< endl;
}
system("PAUSE");
return 0;
}
7. (Program) Write a C++ program that accepts both a string and a single character from the
user. The program should determine how many times the character is contained in the string.
(Hint: Search the string by using the find(str,ƒind) method. This method should be used
in a loop that starts the index value at 0 and then changes the index value to 1 past the index
of where the char was last found.)
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i, number = 0;
string str;
char letter;
cin >> str;
cin >> letter;
int numChars = int(str.length());
cout << "The original string is " << str << endl;
for (i = 0; i < numChars; i++){
if (str.at(i) == letter){
number++;
}
}
cout << "The number of letters is " << number << endl;
system("PAUSE");
return 0;
}
8. (Practice) Enter and run Program 14.6.
//Done!!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "This cannot be";
cout << "The original string is : " << str << endl
<< " and has " << int(str.length()) << " characters." << endl;
// insert characters
str.insert(4, " I know");
cout << "The string, after insertion, is: " << str << endl
<< " and has " << int(str.length()) << " characters." << endl;
// replace characters
str.replace(12, 6, "to");
cout << "The string, after replacement, is: " << str << endl
<< " and has " << int(str.length()) << " characters." << endl;
// append characters
str = str + " correct";
cout << "The string, after appending, is: " << str << endl
<< " and has " << int(str.length()) << " characters." << endl;
system("PAUSE");
return 0;
}
9. (Practice) Enter and run Program 14.7.
//Done!!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1 = "LINEAR PROGRAMMING THEORY";
string s1, s2, s3;
int j, k;
cout << "The original string is " << string1 << endl;
j = int(string1.find('I'));
cout << " The first position of an 'I' is " << j << endl;
k = int(string1.find('I', (j + 1)));
cout << " The next position of an 'I' is " << k << endl;
j = int(string1.find("THEORY"));
cout << " The first location of \"THEORY\" is " << j << endl;
k = int(string1.find("ING"));
cout << " The first index of \"ING\" is " << k << endl;
// now extract three substrings
s1 = string1.substr(2, 5);
s2 = string1.substr(19, 3);
s3 = string1.substr(6, 8);
cout << "The substrings extracted are : " << endl
<< " " << s1 + s2 + s3 << endl;
system("PAUSE");
return 0;
}