Remove Last Character From a String in C++

Namita Chaudhary Jan 30, 2023 Mar 05, 2022
  1. Remove Last Character From a String in C++
  2. Use pop_back() Function to Remove Last Character From the String in C++
  3. Use the erase() Method to Remove Last Character From the String in C++
  4. Remove the Last Character in a String Using the erase(pos,Len) Function
  5. Remove the Last Character in a String Using the erase(iterator) Function
  6. Remove the Last Character From a String Using the substr() Method
  7. Remove the Last Character From a String Using the resize() Method
  8. Conclusion
Remove Last Character From a String in C++

String in C++ is used to store a sequence of characters to perform different operations on it. In this article, we will use strings and remove the last character from it.

Remove Last Character From a String in C++

In C++, a string can be declared in two ways: an array of characters or the standard string class. However, you cannot remove the last element from the array of characters because an array has a fixed size and is not an instantiated object.

The string class in c++ contains several built-in functions that give the string after removing its last character. pop_back() function and erase() function are two of them.

Use pop_back() Function to Remove Last Character From the String in C++

The pop_back() is a built-in function in C++ STL that removes the last element from a string. It simply deletes the last element and adjusts the length of the string accordingly.

Code Snippet:

#include <iostream>
using namespace std;

int main()
{
   string str;
   cin >> str;
   cout<<"Original String: "<<str<<endl;
   str.pop_back();
   cout<<"Required String: "<<str<<endl;
   return 0;
}

Output:

Original String: Welcome
Required String: Welcom

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

The erase() method is a built-in method of the string class. This method can delete a single character or a range of characters by specifying their indexes.

There are three different variations in which the erase() method can be used, but we’ll discuss two of them since we only need to remove the last character.

Remove the Last Character in a String Using the erase(pos,Len) Function

In this function, two parameters are given, and one is the pos that specifies the character to be removed with its index. The second is the Len which tells us the length or the number of characters to be removed from the string.

Code Snippet:

#include<iostream>
using namespace std;

int main()
{
   string str;
   cin>>str;
   cout<<"Original String: "<<str<<endl;
   str.erase(str.size()-1,1);
   cout<<"Required String: "<<str<<endl;
   return 0;
}

Output:

Original String: Hello
Required String: Hell

Remove the Last Character in a String Using the erase(iterator) Function

This function takes only one parameter, an iterator that points to the character to be removed. Therefore, we’ll pass size()-1 as the parameter because since the indexing starts from 0 to size()-1 will point to the last character of the string.

Code Snippet:

#include<iostream>
using namespace std;

int main()
{
    string str;
    cin>>str;
    cout<<"Original String: "<<str<<endl;
    str.erase(str.size()-1);
    cout<<"Required String: "<<str<<endl;
    return 0;
}

Output:

Original String: Hello
Required String: Hell

Remove the Last Character From a String Using the substr() Method

The substr() method returns a substring from the original string. It takes two parameters, the substring’s starting index and the substring’s length that we want.

If you do not provide the starting index, it will return the default value 0, and if the length is not specified, it will take all the characters of the string. The size() method gives the length of the string while size()-1 will provide you the length till the second last character.

Code Snippet:

#include <iostream>
using namespace std;

int main() {
   string str;
   cin >> str;
   cout<<"Original String: "<<str<<endl;
   cout<<"Required String: "<<str.substr(0, str.size() - 1)<<endl;
   return 0;
}

Output:

Original String: Welcome
Required String: Welcom

One important point of this function is that it does not modify the original string but creates a new one.

Remove the Last Character From a String Using the resize() Method

The resize() method resizes the string and alters the content by inserting or removing elements from the container. Since we need to reduce the length of the string by 1, therefore, we can use it.

We need to pass the length of the string that we want. As described previously, size-1 will give the string by removing its last character.

Code Snippet:

#include <iostream>
using namespace std;

int main()
{
    string str;
    cin >> str;
    cout<<"Original String: "<<str<<endl;
    str.resize(str.size()- 1);
    cout<<"Required String: "<<str<<endl;
    return 0;
}

Output:

Original String: Welcome
Required String: Welcom

Conclusion

In this tutorial, we have talked about four different methods by which we can remove the last character from our string and show code examples using the methods mentioned above.

We learned about the syntax of pop_back(), erase() and resize() and substr() methods of Standard Template Library(STL) of C++.

Related Article - C++ String