1

Replace Last Character of a String in C++

 2 years ago
source link: https://thispointer.com/replace-last-character-of-a-string-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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"
"Last City"

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

"Last CitX"
"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

vid5e6258f9da92c874459691.jpg?cbuster=1600267117
00:00/11:46
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY1MTU5NDU4MvZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDM1MxQmMDMmNUYmMTM5N0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwpkNTU1NTYkOTAjJzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NTE1OTQ1ODQmMwtzqWyxPVNyn2yhZG9TUGkurWVlNwI3MTU1NTZuY2JyZvZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZlZXBfYWNyLWkup3QgY2uupzFwqGVlLW9zLWEgp3RlnW5aLWyhLWMyMxYzZzkiYXRTqGF0qXM9ZzFfp2UzZWyxp3A9pHJyYzyx

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;
#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
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;
#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
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;
#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
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;
#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
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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK