Remove leading zeros from a Number given as a string
Last Updated :
05 Oct, 2022
Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single “0”.
Examples:
Input: str = “0001234”
Output: 1234
Explanation:
Removal of leading substring “000” modifies the string to “1234”.
Hence, the final answer is “1234”.
Input: str = “00000000”
Output: 0
Explanation:
There are no numbers except 0
Naive Approach:
The simplest approach to solve the problem is to traverse the string up to the first non-zero character present in the string and store the remaining string starting from that index as the answer. If the entire string is traversed, it means all the characters in the string are ‘0’. For this case, store “0” as the answer. Print the final answer.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
string removeLeadingZeros(string num)
{
for ( int i = 0; i < num.length(); i++) {
if (num[i] != '0' ) {
string res = num.substr(i);
return res;
}
}
return "0" ;
}
int main()
{
string num = "1023" ;
cout << removeLeadingZeros(num) << endl;
num = "00123" ;
cout << removeLeadingZeros(num) << endl;
}
|
Java
import java.io.*;
class GFG
{
static String removeLeadingZeros(String num)
{
for ( int i= 0 ;i<num.length();i++){
if (num.charAt(i)!= '0' ){
String res = num.substring(i);
return res;
}
}
return "0" ;
}
public static void main(String[] args)
{
String num = "1023" ;
System.out.println(removeLeadingZeros(num));
num = "00123" ;
System.out.println(removeLeadingZeros(num));
}
}
|
Python3
def removeLeadingZeros(num):
for i in range ( len (num)):
if num[i] ! = '0' :
res = num[i::];
return res;
return "0" ;
num = "1023" ;
print (removeLeadingZeros(num));
num = "00123" ;
print (removeLeadingZeros(num));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static string removeLeadingZeros( string num)
{
for ( int i = 0; i < num.Length; i++){
if (num[i] != '0' )
{
string res = num.Substring(i);
return res;
}
}
return "0" ;
}
public static void Main( string [] args)
{
string num = "1023" ;
Console.WriteLine(removeLeadingZeros(num));
num = "00123" ;
Console.WriteLine(removeLeadingZeros(num));
}
}
|
Javascript
function removeLeadingZeros(num)
{
for ( var i = 0; i < num.length; i++) {
if (num.charAt(i) != '0' ) {
let res = num.substr(i);
return res;
}
}
return "0" ;
}
let num = "1023" ;
console.log(removeLeadingZeros(num));
num = "00123" ;
console.log(removeLeadingZeros(num));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Space-Efficient Approach:
Follow the steps below to solve the problem in constant space using Regular Expression:
- Create a Regular Expression as given below to remove the leading zeros
regex = “^0+(?!$)”
where:
^0+ match one or more zeros from the beginning of the string.
(?!$) is a negative look-ahead expression, where “$” means the end of the string.
- Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String.
- To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter.
- This method replaces the matched value with the given string.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
void removeLeadingZeros(string str)
{
const regex pattern( "^0+(?!$)" );
str = regex_replace(str, pattern, "" );
cout << str;
}
int main()
{
string str = "0001234" ;
removeLeadingZeros(str);
return 0;
}
|
Java
import java.util.regex.*;
class GFG
{
public static void removeLeadingZeros(String str)
{
String regex = "^0+(?!$)" ;
str = str.replaceAll(regex, "" );
System.out.println(str);
}
public static void main(String args[])
{
String str = "0001234" ;
removeLeadingZeros(str);
}
}
|
Python3
import re
def removeLeadingZeros( str ):
regex = "^0+(?!$)"
str = re.sub(regex, "", str )
print ( str )
str = "0001234"
removeLeadingZeros( str )
|
C#
using System;
using System.Text.RegularExpressions;
class GFG{
public static void removeLeadingZeros( string str)
{
string regex = "^0+(?!$)" ;
str = Regex.Replace(str, regex, "" );
Console.WriteLine(str);
}
public static void Main( string [] args)
{
string str = "0001234" ;
removeLeadingZeros(str);
}
}
|
Javascript
<script>
function removeLeadingZeros(str)
{
const regex = new RegExp( "^0+(?!$)" , 'g' );
str = str.replaceAll(regex, "" );
document.write(str);
}
let str = "0001234" ;
removeLeadingZeros(str);
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Java-specific approach: Refer to this article for the Java-specific approach using StringBuffer.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...