Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Indexing a String | Text Data Type
C++ Data Types
course content

Зміст курсу

C++ Data Types

C++ Data Types

1. Introduction
2. Numerical Data Types
3. Text Data Type
4. Other Data Types and Concepts

Indexing a String

We can access the elements of a string (which are chars) 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.

cpp

main

copy
1234567
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word[12]; // Word's length is 10 }

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.

cpp

main

copy
123456789101112
#include <iostream> int main() { std::string word = "Codefinity"; int index = 12; if (index >= word.length()) std::cout << "The output is unexpected!" << std::endl; else std::cout << word[index]; // Word's length is 10 }

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.

cpp

main

copy
123456789
#include <iostream> using namespace std; int main() { string word = "Codefinity"; cout << word.at(12); // word's length is 10 return 0; }

Try outputting the character with the index 5 in the code window above.

Let's sum up what we used in this chapter:

Practice

As was said before, we can replace the characters inside a string using indexing (both with [] and .at()). Here is an example:

cpp

main

copy
1234567891011
#include <iostream> using namespace std; int main() { string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length()-1) = 'y'; // length-1 accesses the last element cout << word; return 0; }

Завдання

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 of str.

Завдання

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 of str.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 3
toggle bottom row

Indexing a String

We can access the elements of a string (which are chars) 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.

cpp

main

copy
1234567
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word[12]; // Word's length is 10 }

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.

cpp

main

copy
123456789101112
#include <iostream> int main() { std::string word = "Codefinity"; int index = 12; if (index >= word.length()) std::cout << "The output is unexpected!" << std::endl; else std::cout << word[index]; // Word's length is 10 }

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.

cpp

main

copy
123456789
#include <iostream> using namespace std; int main() { string word = "Codefinity"; cout << word.at(12); // word's length is 10 return 0; }

Try outputting the character with the index 5 in the code window above.

Let's sum up what we used in this chapter:

Practice

As was said before, we can replace the characters inside a string using indexing (both with [] and .at()). Here is an example:

cpp

main

copy
1234567891011
#include <iostream> using namespace std; int main() { string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length()-1) = 'y'; // length-1 accesses the last element cout << word; return 0; }

Завдання

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 of str.

Завдання

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 of str.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 3
toggle bottom row

Indexing a String

We can access the elements of a string (which are chars) 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.

cpp

main

copy
1234567
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word[12]; // Word's length is 10 }

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.

cpp

main

copy
123456789101112
#include <iostream> int main() { std::string word = "Codefinity"; int index = 12; if (index >= word.length()) std::cout << "The output is unexpected!" << std::endl; else std::cout << word[index]; // Word's length is 10 }

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.

cpp

main

copy
123456789
#include <iostream> using namespace std; int main() { string word = "Codefinity"; cout << word.at(12); // word's length is 10 return 0; }

Try outputting the character with the index 5 in the code window above.

Let's sum up what we used in this chapter:

Practice

As was said before, we can replace the characters inside a string using indexing (both with [] and .at()). Here is an example:

cpp

main

copy
1234567891011
#include <iostream> using namespace std; int main() { string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length()-1) = 'y'; // length-1 accesses the last element cout << word; return 0; }

Завдання

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 of str.

Завдання

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 of str.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

We can access the elements of a string (which are chars) 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.

cpp

main

copy
1234567
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word[12]; // Word's length is 10 }

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.

cpp

main

copy
123456789101112
#include <iostream> int main() { std::string word = "Codefinity"; int index = 12; if (index >= word.length()) std::cout << "The output is unexpected!" << std::endl; else std::cout << word[index]; // Word's length is 10 }

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.

cpp

main

copy
123456789
#include <iostream> using namespace std; int main() { string word = "Codefinity"; cout << word.at(12); // word's length is 10 return 0; }

Try outputting the character with the index 5 in the code window above.

Let's sum up what we used in this chapter:

Practice

As was said before, we can replace the characters inside a string using indexing (both with [] and .at()). Here is an example:

cpp

main

copy
1234567891011
#include <iostream> using namespace std; int main() { string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length()-1) = 'y'; // length-1 accesses the last element cout << word; return 0; }

Завдання

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 of str.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 3
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt