Replace First Character in a string in C++

In this article, we will discuss different ways to replace first 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 first character of this string with character ‘X’. The final string should be like this,

"Xast City"

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

Using subscript operator

We can access the characters in a string by their index positions using the subscript operator i.e. []. To replace the value of first character of string, just select the character at index position 0 and assign new value to it.

For Example:

#include <iostream>
#include <string>

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

    // Replace first character of string
    strValue[0] = 'X';

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

    return 0;
}

Output:

Xast City

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

Using string::replace() function

In C++, the string class provides a member function replace() to replace the contents of string. Using one of its overloaded version, we can replace the first character of string. For that, we need to pass the following arguments to the replace() function,

  • Index position from where we want to start replacing the characters. In our case it will be 0.
  • Number of characters that need to be replaced. As we want to replace only first 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 first character of string
    strValue.replace(0, 1, "X");

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

    return 0;
}

Output:

Xast City

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

Using string::at() function

In C++, the string class provides a member function at(). It takes an index position as argument and returns the reference to character at that index position. We can use this to get the reference to first character of string and then change its value.

For Example:

#include <iostream>
#include <string>

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

    // Replace first character of string
    strValue.at(0) = 'X';

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

    return 0;
}

Output:

Xast City

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

Important point: If the index position provided in at() function is invalid, then it will raise an std::out_of_range exception.

Using string::substr()

In C++, the string class provides a member function substr(), to select a substring from the string. We can select a substring that contains all the characters from string except the first one. Then append this substring to “X”.

For Example:*

#include <iostream>
#include <string>

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

    // Replace first character of string
    strValue = "X" + strValue.substr(1);

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

    return 0;
}

Output:

Xast City

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

How did it work?

We passed the index position 1 as argument to the substr() function. It selected the characters from index position 1 till the end of string. Then we appended this substring to “X” and assigned it back to the original string variable.

Summary:

We learned about four different ways to replace first 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