Note 7.3.1.
In C++, indexing begins at 0!
[
and ]
) for this operation.
fruit
using [
and ]
.fruit[1]
indicates that I want character number 1 from the string named fruit
. The result is stored in a char
named letter
. When I output the value of letter
, I get a surprise:
a
a
is not the first letter of "banana"
. Unless you are a computer scientist. For perverse reasons, computer scientists always start counting from zero. The 0th letter (“zeroeth”) of "banana"
is b
. The 1th letter (“oneth”) is a
and the 2th (“twoeth”) letter is n
.
fruit
.#include <iostream>
using namespace std;
int main() {
string bake = "bake a cake!";
char letter = bake[?];
}
#include <iostream>
using namespace std;
int main() {
string lunch = "hello";
string person = "deejay";
lunch[0] = lunch[3];
cout << lunch;
}
cout
a string
we print its content not its name.