Replace Last Character of a String in C++

In this article, we will discuss different ways to replace last character of a string with another character in C++.

Table Of Contents

Suppose we have a string like this,

"Last City"

We want to replace the last character of this string with character ‘X’. The final string should be like this,

"Last CitX"

There are different ways to replace only the last character of a string in C++. Let’s discuss them one by one,

Advertisements

Using subscript operator

We can use the subscript operator i.e. [] with string object, to access the characters in a string by their index positions. To replace the value of last character of string, just select the character at index position N-1, where N is the size of string. Then assign new value to it.

For Example:

#include <iostream>
#include <string>

int main()
{
    std::string strValue = "Last City";

    // Replace last character of string
    strValue[strValue.size() - 1] = 'X';

    std::cout<< strValue << std::endl;

    return 0;
}

Output:

Last CitX

It replaced the last character of a string with character ‘X’.

Using string::replace() function

In C++, the string class provides a function replace() to change the contents of string based on index positions. Using one of its overloaded version, we can replace the last character of string. For that, we need to pass the following arguments to the replace() function,

  • Index position from where replacement should start. In our case it will be N-1, where N is the size of string.
  • Number of characters to be replaced. As we want to replace only the last character, so it will be 1.
  • The replacement string. It will be “X” in our case.

Let’s the complete example,

#include <iostream>
#include <string>

int main()
{
    std::string strValue = "Last City";

    // Replace last character of string
    strValue.replace(strValue.size() - 1, 1, "X");

    std::cout<< strValue << std::endl;

    return 0;
}

Output:

Last CitX

It replaced the last character of a string with character ‘X’.

Using string::at() function

In C++, the string class provides a function at(). It takes an index position as argument and returns the reference to character at that index position. If the given index position is invalid, then it raises an std::out_of_range exception.

We can use this to get the reference to last character of string and then change its value.

For Example:

#include <iostream>
#include <string>

int main()
{
    std::string strValue = "Last City";

    // Replace last character of string
    strValue.at(strValue.size() - 1) = 'X';

    std::cout<< strValue << std::endl;

    return 0;
}

Output:

Last CitX

It replaced the last character of a string with character ‘X’.

Using string::substr()

Using the substr() function of string class, select a substring that contains all the characters from string except the last one. Then “X” to that substring.

For Example:*

#include <iostream>
#include <string>

int main()
{
    std::string strValue = "Last City";

    // Replace last character of string
    strValue = strValue.substr(0, strValue.size() - 1) + "X";

    std::cout<< strValue << std::endl;

    return 0;
}

Output:

Last CitX

It replaced the last character of a string with character ‘X’.

How did it work?

We passed the index position 0 and strValue.size() – 1 as argument to the substr() function. It selected the characters from index position 0 till the second last character of string. Then we appended “X” to that substring and assigned it back to the original string variable.

Summary:

We learned about four different ways to replace the last character of a string in C++.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top