• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
September 23, 2022 |1.4K Views
C++ Program to Remove the Leading Zeros
  Share  2 Likes
Description
Discussion

In this video, we will write a C++ Program to Remove the Leading Zeros. As we know a string is a collection of characters and a string class is part of the library.

Here we take numeric string str and remove all the leading zeros from a given string. If the string contains only zeros, then print a single “0”.

Examples:
Input string: ‘000012309’
Output: 12309

Input string: ‘00GFG’
Output: GFG

Here we used two different methods to remove the leading zeros:

1. Using str.erase( ): This function is used for erasing a part of the string content, shortening the length of the string. 
Syntax: string& string ::erase ()

2. Using stoi(str): In this method, we use stoi() function, which takes a string as an argument and returns its value in integer form. So we can easily convert strings into integers and remove the leading zeros. This approach is popular in current versions of C++.

Related Article : https://www.geeksforgeeks.org/remove-leading-zeros-from-string-in-cpp/

Read More