Remove Last Character From String in C++

Remove last character from String in C++

C++ is one of the most widely used programming languages. While programming in C++, we often come across text data. To represent text data in C++, we use string data type or an array of characters. In this article, we will discuss different ways to remove the last character from a string in C++. We will also discuss different approaches to remove the last character from a char array, a stringstream, and a cstring in C++.

How to Remove the Last Character From String in C++?

C++ provides us with a string object to store the sequence of characters. It is an extension of the character array. However, the string is a class that defines an object and the characters are stored as a sequence of bytes. To use the string class, we shall include the <string> header file in our code. 

There are different methods to remove the last character from a string in C++. Let us understand each method one by one.

Remove the Last Character From String in C++ Using the pop_back() Method

The string class provides us with methods to add as well as remove characters from a string. The pop_back() method of the string class pops up the last character from the string and reduces the length of the string by one. The method does not return anything and also does not accept any arguments. We can invoke the pop_back() method using a string object. The function signature of the pop_back() method is defined as follows.

You can use the pop_back() method to remove the last character of a string in C++ as follows.

Output:

In the output, you can observe the last character ‘1’ is removed. You should note that we can initiate a string using the string object constructor. In essence, we can pass the string argument to the string object while declaring it. 

Remove the Last Character From String in C++ Using the substr() Method

The substr() method is defined in the string class. We can use this method to generate a substring of a string. We need to provide two input arguments to the substr() method. The first argument represents the starting index (we have 0-indexed strings in C++) and the second argument represents the length of the substring. 

Following is the definition of the substr() method. 

We can see the method returns a string that is a substring of the original string. 

To remove the last character from the string using the substr() method, We will invoke the substr() method on the original string object. After that, we will pass the first argument as 0 and the second argument is one less than the length of the original string.

We can get the length of a string using the ‘size()’ method of the string object. This method returns an integer that is the total length of the string. The total length of a string is the count of characters in the string including whitespaces.

Using the above approach, you can remove the last character from a string in C++ using the substr() method as follows.

Output:

Remove the Last Character From String in C++ Using the resize() Method

The string resize() method is yet another method we can use to remove the last character from the string. This method is implemented in the string class. We can pass an integer as an argument to this method and the string is resized to the size of the passed integer. If the passed integer is less than the size of the original string, it is trimmed from the end. You should note that this method changes the string in place. Therefore, we do not need to re-assign the results to string.  You can check the resize() method that is defined as given below.

To remove the last character from the string using the resize() method, we will invoke the string resize() method on the original string. Also, we will pass one less than the size of the string as the argument. Again, you can get the length of the original string using the size() method.

Following is the code to remove the last character of a string using the resize() method in C++.

Output:

Remove the Last Character From String in C++ Using the erase() Method

To remove the last character of a string in C++, We can also use the string erase() method that is implemented in the C++ string class. This method erases the part of the string and reduces its length. We can use this method to erase the last character from the string. Thus the remaining string will be our desired string. The erase() method is also the in-place method therefore we do not need to reassign the string. 

To remove the last character using the erase() method, we will invoke the erase() method on the original string. Also, we will pass one less than the size of the original string as the input argument.

The erase() method removes the character to which the iterator is passed. For example when we pass ‘str.size()-1’, we pass the iterator to the last character. Therefore, the last character will be removed. You can check the erase() method that is defined as given below.

You can remove the last character of a string using the erase() method in C++ as follows.

Output:

As a word of caution, if you think that your string might be an empty string i.e. its size might be zero, you should always check it before removing the character. If the string is already empty, the operation to remove the last character will cause a runtime error. 

Let us now understand how we can remove the last character from a character array.

Remove the Last Character From Character Array in C++

Character arrays are a classic way to store text data. We can store a sequence of characters into a character array. To mark the end of the text data or a character string, the last element in a character array is the null character ‘\0’. Therefore, to remove the last character from a character array, we can effectively move the null character to one place left. In this way, our character array will be terminated with one character less than the original character array. 

So to remove the last character from the character array, we will perform the following steps.

  • We will iterate the array until we find the null character ‘\0’ to get the size of the array. 
  • We will put the null character ‘\0’ at the second last place.

You should note that the last place in the character array is for the null character so the last readable character is actually at the second-last place. So we shall insert the null character at the second last place to remove the last character.

Following is the code to remove the last character from a char array in C++.

Output:

Note that as soon as you input space as a character, the ‘cin’ automatically puts a null character to the character array, and content after that is not stored in the character array. If you want to input space as well, you should use the ‘getline()’ function instead of ‘cin’.

Remove the Last Character From stringstream in C++

C++ provides us with the stringstream class. It enables us to perform read and write operations on a string as if the string is being read from or written to the console. Therefore, we can make use of stringstream to perform operations on the string. To remove the last character from the string, we will use the seekp() method of the stringstream class. 

The seekp() method sets the position of the cursor to the place where we want to insert the character. The seekp() method, that we will use, accepts two arguments. The first argument is the stream offset that dictates the position. The second argument that we pass to the method is a standard flag that dictates the direction of movement of the cursor. You can check the seekp() method that is defined as given below.

The ostream seekp() is found in stringstream as well because of the inheritance. 

To remove the last character from the stringstream, we will follow the following steps.

  • We will create a string stream and write our string to it.
  • We will then invoke the seekp() method with two parameters.
  • The first argument is ‘-1’ that points to the first readable position from the last. It means that the cursor shall be moved to the last character of the string.
  • As the second argument, we will pass the ‘std::ios_base::end’ flag. It indicates that the movement of the cursor is from the last position towards the first position. 
  • We will replace the last character with the null character ‘\0’.
  • Lastly,  we will write the stringstream back to the string.

Following is the code to remove the last character of a stringstream in C++.

Output:

You should note that when we remove the last character from stringstream, we are actually making the C++ string similar to the C-style string. This is because we have interested in the null character at the end of the string. The C-style strings or cstrings are similar to the C++ string objects except that they have a null character at the end. However, the C-style strings are actually a character array rather than a string object.  Therefore, we can remove the last character from a C-style string (cstring) in a similar fashion as we have done for the character array. To use the cstring functions we should include the <cstring> header file in our code. 

Conclusion

In C++, we use strings to store the text data. Among many other operations, an important operation on the strings is to remove the last character. In this article, we have understood different approaches to remove the last character from a C++ string. You should always be cautious about the size of the string before operating on it. It is because removing characters from empty strings might cause the program to fail. 

Further, we have seen the methods to remove the last character from a character array. Since C-strings are similar to a character array, the method for character array can be copied to C-style strings as well. The streams come in handy in different situations and are popularly used while reading and writing files. In this article, we have also understood the stringstream and how we can remove the last character from it. 

That’s all about how to remove last character from String in C++.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Was this post helpful?

Leave a Reply

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