Remove Leading Zeros From String in C++ Last Updated : 14 Sep, 2022 Suggest changes Share 4 Likes 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 K kartik 4 Article Tags : C++ cpp-string CPP Strings Programs Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 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++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like