int main() {
string quote = "I love you 3000.";
int x = 3;
int y = 3 * x;
int z = 1;
if (y > 12) {
z = z + x + y;
}
else {
z = z + y - x;
}
cout << quote[z];
}
I
The value of z is not 0.
0
The value of z is not greater than 11.
o
The value of z is not 3.
y
The final value of z is 7, and ‘y’ is at index 7 of quote.
int main() {
string quote = "With great power comes great responsiblity.";
size_t n = 0;
while (n < quote.length()) {
if (n % 5 == 0) {
cout << quote[n];
}
n++;
}
}
teeest
Remember that indexing begins at 0 in C++.
Wg reeest
If we print out every fifth character, including the first, this is the answer.
ith reatpowe coms grat rsponibliy.
This is what we would get if we removed every fifth character.
int main() {
string quote = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";
size_t index = quote.find("wood");
cout << index;
}
4
Although “wood” appears four times in the string, that is not what the find function returns.
9
The index of ‘w’ in the first “wood” is at index 9.
10
Remember indexing begins at 0 in C++.
12
The find function returns the index of the first character of the found string.
22
The find function returns the index of the first character of the found string.
int main() {
string quote = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";
size_t index = quote.find('w', quote.find("wood") + 1);
cout << index;
}
9
Take a closer look at the starting index for where we should start looking.
22
After the first ‘w’, the second ‘w’ appears at index 22.
43
Take a closer look at the find function and its arguments.
65
Take a closer look at the find function and its arguments.
int main() {
string quote = "Life is like a box of chocolates. You never know what you're gonna get.";
size_t i = 0;
size_t count = 0;
while (i < quote.length()) {
if (quote[i] == 'e') {
count++;
}
i++;
}
cout << count;
}
0
Are there any occurences of the letter ‘e’ in quote?
6
Count the number of ‘e’s in quote.
7
There are 7 occurences of the letter ‘e’ in quote.
An error occured while delivering a message. All instances of the letter ‘s’ got replaced by ‘X’s. Can you complete the code below to fix this error by selecting the correct line of code to replace the question marks?