Remove Leading Zeros from a Number in C++

This tutorial topic is: How to remove leading zeros from a number or string in C++. For example, if you have a string or number which contains zeros as prefix, then you can easily remove those prefix zeros in C++ with this program.

We can implement the above problem in linear time complexity using string. So, look at the below approach.

Approach :

Here we will take the number in a string format. We will run a loop where we will trace the first index where there is no ‘0’ in that string. We will simply print the whole string from there. Thus, we can remove all the leading zeros.

Code: Remove leading zeros from a number in C++

Here is the code of the above approach in C++.


#include <bits/stdc++.h>
using namespace std;

int main()
   char s[10001];//size will be 10000(10^1000)
   cin>>s;// taking input of string
   long long int i,c=-1;
       if(s[i]!='0'){// check the first index where 0 doesnt occur
       cout<<s[i];// print the rest of the string

// Input : 0000012050
// Output : 12050

Time Complexity : 

Complexity would be O(n), where n is the length of the string.

Hope, you have enjoyed the article, thanks for reading.

Also, have a look at these:

Leave a Reply

Your email address will not be published. Required fields are marked *