Remove Leading Zeros From String in C++ Last Updated : 14 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given a string of digits, remove leading zeros from it. Examples: Input : 00000123569 Output : 123569 Input : 000012356090 Output : 12356090 In this article, we will use two string functions i.e, string erase and stoi() to remove leading zeros from the string. 1. Using String Erase FunctionCount trailing zeros. Use the string erase function to remove characters equal to the above count. Below is C++ implementation. CPP // C++ program to remove leading zeros // from a given string #include <iostream> using namespace std; string removeZero(string str) { // Count leading zeros int i = 0; while (str[i] == '0') i++; // The erase function removes i characters // from given index (0 here) str.erase(0, i); return str; } // Driver code int main() { string str; str = "00000123569"; str = removeZero(str); cout << str << endl; return 0; } Output1235692. Using Stoi() Method stoi() function in C++ is used to convert the given string into an integer value. It takes a string as an argument and returns its value in integer form. We can simply use this method to convert our string to an integer value which will remove the leading zeros. s to i() | | |___ integer | |_______ to |__________ String C++ // C++ Program to remove leading zeros // with the help of stoi() function #include <iostream> using namespace std; int main() { string str; str = "00000123569"; int num = stoi(str); str = to_string(num); cout << "String after removing leading zeros is: " << str << endl; return 0; } OutputString after removing leading zeros is: 123569 C++ Program to Remove the Leading Zeros Comment More infoAdvertise with us K kartik Improve Article Tags : C++ cpp-string CPP Strings Programs Explore Introduction to C++Introduction to C++ Programming Language2 min readHeader Files in C++5 min readSetting up C++ Development Environment8 min readDifference between C and C++3 min readBasicsC++ Data Types7 min readC++ Variables4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readC++ Loops7 min readFunctions in C++8 min readC++ Arrays8 min readStrings in C++5 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readC++ OOPInheritance in C++10 min readC++ Polymorphism5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Containers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice C++C++ Interview Questions and Answers1 min readTop C++ DSA Related ProblemsC++ Programming Examples7 min read Like