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,

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++.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top