Move all digits to the beginning of a given string
Last Updated :
08 Feb, 2023
Given a string S, the task is to move all the digits present in the string, to the beginning of the string.
Examples:
Input: S = “Geeks4forGeeks123”
Output: 4123GeeksforGeeks
Explanation:
The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the string to “4123GeeksforGeeks”.
Input: S = “GeeksforGeeks1234 A Com56puter Science Port7al”
Output: 1234567GeeksforGeeks A Computer Science Portal
Approach: The idea is to traverse the string and maintain two strings, one string contains the digits and another string contains non-numeric characters. In the end, append both the strings to the result in the required order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void moveAllDigitAtBeginning(
string str)
{
int len = str.size();
string digits = "" ;
string nonNumericCharacter = "" ;
for ( int i = 0; i < str.size(); i++)
{
char c = str[i];
if (c >= 48 && c <= 57)
{
digits += c;
}
else
{
nonNumericCharacter += c;
}
}
digits += nonNumericCharacter;
cout << digits << endl;
}
int main()
{
string str = "GeeksforGeeks123" ;
moveAllDigitAtBeginning(str);
return 0;
}
|
Java
class GFG {
static void moveAllDigitAtBeginning(
String str)
{
int len = str.length();
StringBuilder digits
= new StringBuilder();
StringBuilder nonNumericCharacter
= new StringBuilder();
for ( char c : str.toCharArray()) {
if (c >= 48 && c <= 57 ) {
digits.append(c);
}
else {
nonNumericCharacter.append(c);
}
}
digits.append(
nonNumericCharacter.toString());
System.out.print(
digits.toString() + " " );
}
public static void main(String args[])
{
String str
= "GeeksforGeeks123" ;
moveAllDigitAtBeginning(str);
}
}
|
Python3
def moveAllDigitAtBeginning( str ):
Len = len ( str )
digits = ""
nonNumericCharacter = ""
for i in range ( len ( str )):
c = str [i]
if ( ord (c)> = 48 and ord (c)< = 57 ):
digits + = c
else :
nonNumericCharacter + = c
digits + = nonNumericCharacter
print (digits)
str = "GeeksforGeeks123"
moveAllDigitAtBeginning( str )
|
C#
using System;
public class GFG{
static void moveAllDigitAtBeginning( string str)
{
int len = str.Length;
string digits = "" ;
string nonNumericCharacter = "" ;
for ( int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c >= 48 && c <= 57)
{
digits += c;
}
else
{
nonNumericCharacter += c;
}
}
digits += nonNumericCharacter;
Console.WriteLine(digits);
}
static public void Main ()
{
string str = "GeeksforGeeks123" ;
moveAllDigitAtBeginning(str);
}
}
|
Javascript
<script>
function moveAllDigitAtBeginning(str)
{
var len = str.length;
var digits = [];
var nonNumericCharacter = [];
var temp = str.split( "" );
for (const c of temp)
{
if (c.charCodeAt(0) >= 48 &&
c.charCodeAt(0) <= 57)
{
digits.push(c);
}
else
{
nonNumericCharacter.push(c);
}
}
digits.push(nonNumericCharacter.join( "" ));
document.write(digits.join( "" ) + " " );
}
var str = "GeeksforGeeks123" ;
moveAllDigitAtBeginning(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Regular Expression based Approach: The given problem can also be solved using Regular Expression and replace all non-numeric characters with the empty string (“ “) that give you all numbers and replace all digits with the empty string (“ “) that gives you all non-numeric characters. In the end, concatenate the string and print the result.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
#include <regex>
using namespace std;
void moveAllDigitAtBeginning(string str)
{
string moveAllDigit =
regex_replace(str, regex( "\\D+" ), "" )
+ regex_replace(str, regex( "\\d" ), "" );
cout << moveAllDigit;
}
int main()
{
string str = "GeeksforGeeks1234" ;
moveAllDigitAtBeginning(str);
return 0;
}
|
Java
class GFG {
static void moveAllDigitAtBeginning(
String str)
{
String moveAllDigit = str.replaceAll( "\\D+" , "" )
+ str.replaceAll( "\\d+" , "" );
System.out.println(moveAllDigit);
}
public static void main(String args[])
{
String str
= "GeeksforGeeks1234" ;
moveAllDigitAtBeginning(str);
}
}
|
Python3
import re
def moveAllDigitAtBeginning( str ):
moveAllDigit = re.sub(r '\D+' ," ", str) + re.sub(r'\d+'," ", str )
print (moveAllDigit)
if __name__ = = "__main__" :
str = "GeeksforGeeks1234"
moveAllDigitAtBeginning( str )
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
static void moveAllDigitAtBeginning(
String str)
{
String moveAllDigit = Regex.Replace(str, "\\D+" , "" )
+Regex.Replace(str, "\\d" , "" );
Console.WriteLine(moveAllDigit);
}
public static void Main(String []args)
{
String str
= "GeeksforGeeks1234" ;
moveAllDigitAtBeginning(str);
}
}
|
Javascript
function moveAllDigitAtBeginning(str) {
let moveAllDigit = str.replace(/\D+/g, "" ) + str.replace(/\d+/g, "" );
console.log(moveAllDigit);
}
let str = "GeeksforGeeks1234" ;
moveAllDigitAtBeginning(str);
|
Output:
1234GeeksforGeeks
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...