Replace a character at a particular index in a string in C++
This post will discuss how to replace a character at a particular index in a string in C++.
1. Using [] operator
The simplest and most intuitive way to replace a character in a string at a given index is to use the indexing operator []. This operator allows us to access and modify any character in a string by its position, starting from zero. For example, the following code replaces the character at index 5 in the given string with a space:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { std::string str = "Hello,World"; int index = 5; char replacement = ' '; str[index] = replacement; std::cout << str << std::endl; // Hello World return 0; } |
The indexing operator is very easy to use and understand. However, it does not check if the index is valid or within the bounds of the string. If we try to access or modify an invalid index, such as a negative number or a number greater than or equal to the length of the string, we will get undefined behavior, which may result in a runtime error, a wrong output, or even a security vulnerability. Therefore, we should only use the indexing operator when we are sure that the index is valid and within the bounds of the string.
2. Using string::at function
Another option to replace a character at a particular index in a string is to use the string::at function. This function is similar to the indexing operator, but with one important difference: it checks if the index is valid and within the bounds of the string. If it is not, it throws an out_of_range exception, which we can catch and handle accordingly. This makes our code more robust and secure, as we can avoid undefined behavior and handle errors gracefully. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <stdexcept> int main() { std::string str = "Hello,World"; int index = 5; char replacement = ' '; try { str.at(index) = replacement; std::cout << str << std::endl; // Hello World } catch (std::out_of_range& e) { std::cout << "Invalid index: " << e.what() << std::endl; } return 0; } |
3. Using string::replace function
The third option to replace a character at a particular index in a string is using the string::replace function. This function allows us to replace a part of a string with another string, starting from a given position and for a given length. For example, the following code replaces the character at index 5 in the string with a space:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> int main() { std::string str = "Hello,World"; int index = 5; str.replace(index, 1, " "); std::cout << str << std::endl; // Hello World return 0; } |
Unlike the indexing operator, the string::replace function throws an exception if the index is out of bounds, the resulting string would exceed the maximum permissible size, memory allocation fails, etc. However, it is more verbose and less intuitive than the indexing operator.
That’s all about replacing a character at a particular index in a string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)