Warning 7.4.1.
A common source of error involving strings and other arrays is indexing out of bounds. This is usually the result of forgetting to subtract 1 from
length.
length function. The syntax for calling this function is a little different from what we’ve seen before.
fruit.fruit. This vocabulary may seem strange, but we will see many more examples where we invoke a function on an object. The syntax for function invocation is called “dot notation,” because the dot (period) separates the name of the object, fruit, from the name of the function, length.
length takes no arguments, as indicated by the empty parentheses (). The return value is an integer, in this case 6. Notice that it is legal to have a variable with the same name as a function.
int length = fruit.length();
char last = fruit[length]; // WRONG!!
"banana". Since we started counting at 0, the 6 letters are numbered from 0 to 5. To get the last character, you have to subtract 1 from length.
length.
fruit using the length function.string s = "coding rocks";
cout << (s[s.length()-5]) << endl;
course being the first variable initialized.