
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Trailing Zeros from String in C++
Trailing zeros are the zero occuring at the end of a string. In this article, we will discuss different approaches to remove trailing zeros from a string. The below section explains the problem statement.
The input of this problem is a non-empty string containing characters and numbers. Our task is to remove all the zero's appering after the last non-zero character in the string. For example:
// Input string "012424000" // Output String "012424"
Remove Trailing Zeros From String
Here is the list of approaches to remove all the trailing zeros from a string using c++ program, which we will be discussing in this article with stepwise explanation and complete example codes.
Using Regular Expression to Remove Trailing Zeros
The regular expression is used to match and edit a string pattern in a larger string. In this method, we will use regex_replace() function to match all trailing zeros inside the string and replace it with empty piece of string. Let's see how to do it below.
Example
In the code below, the regular expression regex("0+$") will only match with all the trailing zeros of string.
#include <iostream> #include <regex> using namespace std; int main() { string str = "0012304000"; str = regex_replace(str, regex("0+$"), ""); cout << str; }
The output of the above code will be:
0012304
Using Reverse Loop
This approach uses a loop to count trailing zeros from the end of string and then trims them of using substr() function. This is the most stringforward approach to remove trailing zeros, also this approch is more flexible and easy to understand.
Example
In the code below, the while loop is running from the end of the string, the condtion i >= 0 && str[i] == '0' will help to reach last non-zero character in string. All the character after the last non-zero character are removed using substr() function.
#include <iostream> using namespace std; int main() { string str = "1234000"; int i = str.length() - 1; while (i >= 0 && str[i] == '0') --i; str = str.substr(0, i + 1); cout << str; }
The output of the above code will be:
1234
Using find_last_not_of() to Remove Trailing Zeros
The find_last_not_of() function is used to find the position of the last character in a string that does not match any character in a specified set. In this method, we can use this function to find last non-zero character in the string and erase everything after it.
Example
In the code below, the all the trailing zeros of the string "123stdg4000" is removed using find_last_not_of function.
#include <iostream> using namespace std; int main() { string str = "123stdg4000"; size_t end = str.find_last_not_of('0'); if (end != string::npos) str.erase(end + 1); else str.clear(); // in case the string is all zeros cout << str; }
The output of the above code will be:
123stdg4