String is a sequence of symbols. All of the symbols can be accessed via their indexes starting from 0.
Let's say we have the string "computer". It's structure is the following:
So if we need to print the first letter (c) and the 3rd letter (m), we do:
Output:
Each string is terminated with a special character '\0'. If printed an empty space is shown.
Example
Output:
Length is the count of the symbols contained in a string.
It's accessed via the length
function.
Example:
Output:
Via length()
we can access the last symbol of a string.
Example:
Output:
Accessing string outside of it's bounds results in exception.
Let's look the following example:
The string has indexes from 0 to 5 (5 is the '\0' symbol). Accessing index outside of that bound (in this case 10) causes exception.
We can check if string is contained in a string via the find
function.
Examples:
Output:
All symbols of a string can be iterated via for loop.
Let's print all letters of a word that are not vowels:
Output:
In this case vowels.find(letter) == string::npos
means that
letter
is a symbol that's not a vowel letter.