Conteúdo do Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
Indexing a String
We can access the elements of a string
(which are char
s) using indexing with square brackets []
just like we would do with an array. Using that, we can also replace a character at a given position.
With that indexing, you can try to access a wrong index (that is out of range), and the compiler will tell you nothing about that.
main.cpp
As you can see, the output is weird, and C++ did not warn you about the problem (as always...). To avoid it, you can get the length of a string
using the .length()
method and ensure your index is smaller than the length of your string.
main.cpp
Another way of indexing is using the .at()
method. To get the character with index n
, we can use the following syntax:
It works the same as str[n]
but will raise an error if you specify an index that is out of range.
main.cpp
Try outputting the character with the index 5
in the code window above.
Let's sum up what we used in this chapter:
Method | Description |
---|---|
str[n] |
Get the character of str with index n |
str.at(n) |
The same as str[n] but raises an error if index is incorrect |
str.length() |
Get the length of the str string |
Practice
As was said before, we can replace the characters inside a string using indexing (both with []
and .at()
). Here is an example:
main.cpp
Tarefa
Your task is to check if we can add characters to the end of a string that way.
- Figure the index of the last character in a string.
- Assign
'y'
to the index index of last character + 1 ofstr
.
Tudo estava claro?
Conteúdo do Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
Indexing a String
We can access the elements of a string
(which are char
s) using indexing with square brackets []
just like we would do with an array. Using that, we can also replace a character at a given position.
With that indexing, you can try to access a wrong index (that is out of range), and the compiler will tell you nothing about that.
main.cpp
As you can see, the output is weird, and C++ did not warn you about the problem (as always...). To avoid it, you can get the length of a string
using the .length()
method and ensure your index is smaller than the length of your string.
main.cpp
Another way of indexing is using the .at()
method. To get the character with index n
, we can use the following syntax:
It works the same as str[n]
but will raise an error if you specify an index that is out of range.
main.cpp
Try outputting the character with the index 5
in the code window above.
Let's sum up what we used in this chapter:
Method | Description |
---|---|
str[n] |
Get the character of str with index n |
str.at(n) |
The same as str[n] but raises an error if index is incorrect |
str.length() |
Get the length of the str string |
Practice
As was said before, we can replace the characters inside a string using indexing (both with []
and .at()
). Here is an example:
main.cpp
Tarefa
Your task is to check if we can add characters to the end of a string that way.
- Figure the index of the last character in a string.
- Assign
'y'
to the index index of last character + 1 ofstr
.
Tudo estava claro?