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

Course Content

C++ Data Types

C++ Data Types

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

bookAccessing and Manipulating String Elements

Indexing a string

We can access the elements of a string (which are essentially char characters) using square bracket [] indexing, similar to arrays. This also allows us to replace a character at a specific position.

h

indexing

copy
12
string str = "Cpdefinity"; // Cpdefinity str[1] = 'o'; // Codefinity

Note

Indexing in a string starts from zero, just like in arrays.

The length of the string

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 unexpected, and C++ does not provide a warning about the issue. To prevent this, you can use the .length() method to determine the length of a string and ensure the index is within the valid range.

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 }

Indexing using method

Another way of indexing is using the .at() method. To get the character with index n, we can use the following syntax:

h

at

copy
1
str.at(n);

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
12345678
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word.at(12); // word's length is 10 // Try outputting the character with the index 5 }

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
123456789
#include <iostream> int main() { std::string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length() - 1) = 'y'; // length - 1 accesses the last element std::cout << word; }
Task
test

Swipe to show code editor

Your task is to check if we can add characters to the end of a string that way.

  1. Figure the index of the last character in a string.
  2. Assign 'y' to the index index of last character + 1 of str.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookAccessing and Manipulating String Elements

Indexing a string

We can access the elements of a string (which are essentially char characters) using square bracket [] indexing, similar to arrays. This also allows us to replace a character at a specific position.

h

indexing

copy
12
string str = "Cpdefinity"; // Cpdefinity str[1] = 'o'; // Codefinity

Note

Indexing in a string starts from zero, just like in arrays.

The length of the string

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 unexpected, and C++ does not provide a warning about the issue. To prevent this, you can use the .length() method to determine the length of a string and ensure the index is within the valid range.

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 }

Indexing using method

Another way of indexing is using the .at() method. To get the character with index n, we can use the following syntax:

h

at

copy
1
str.at(n);

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
12345678
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word.at(12); // word's length is 10 // Try outputting the character with the index 5 }

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
123456789
#include <iostream> int main() { std::string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length() - 1) = 'y'; // length - 1 accesses the last element std::cout << word; }
Task
test

Swipe to show code editor

Your task is to check if we can add characters to the end of a string that way.

  1. Figure the index of the last character in a string.
  2. Assign 'y' to the index index of last character + 1 of str.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookAccessing and Manipulating String Elements

Indexing a string

We can access the elements of a string (which are essentially char characters) using square bracket [] indexing, similar to arrays. This also allows us to replace a character at a specific position.

h

indexing

copy
12
string str = "Cpdefinity"; // Cpdefinity str[1] = 'o'; // Codefinity

Note

Indexing in a string starts from zero, just like in arrays.

The length of the string

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 unexpected, and C++ does not provide a warning about the issue. To prevent this, you can use the .length() method to determine the length of a string and ensure the index is within the valid range.

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 }

Indexing using method

Another way of indexing is using the .at() method. To get the character with index n, we can use the following syntax:

h

at

copy
1
str.at(n);

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
12345678
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word.at(12); // word's length is 10 // Try outputting the character with the index 5 }

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
123456789
#include <iostream> int main() { std::string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length() - 1) = 'y'; // length - 1 accesses the last element std::cout << word; }
Task
test

Swipe to show code editor

Your task is to check if we can add characters to the end of a string that way.

  1. Figure the index of the last character in a string.
  2. Assign 'y' to the index index of last character + 1 of str.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Indexing a string

We can access the elements of a string (which are essentially char characters) using square bracket [] indexing, similar to arrays. This also allows us to replace a character at a specific position.

h

indexing

copy
12
string str = "Cpdefinity"; // Cpdefinity str[1] = 'o'; // Codefinity

Note

Indexing in a string starts from zero, just like in arrays.

The length of the string

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 unexpected, and C++ does not provide a warning about the issue. To prevent this, you can use the .length() method to determine the length of a string and ensure the index is within the valid range.

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 }

Indexing using method

Another way of indexing is using the .at() method. To get the character with index n, we can use the following syntax:

h

at

copy
1
str.at(n);

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
12345678
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word.at(12); // word's length is 10 // Try outputting the character with the index 5 }

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
123456789
#include <iostream> int main() { std::string word = "Cpdefinitu"; word[1] = 'o'; word.at(word.length() - 1) = 'y'; // length - 1 accesses the last element std::cout << word; }
Task
test

Swipe to show code editor

Your task is to check if we can add characters to the end of a string that way.

  1. Figure the index of the last character in a string.
  2. Assign 'y' to the index index of last character + 1 of str.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt