- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Move all digits to the beginning of a given string
In this article, we will explore a common string manipulation problem: moving all digits to the beginning of a given string. This task is often seen in data cleaning or preprocessing, where we need to standardize or reformat strings in a certain way. A widely-used programming language celebrated for its efficiency and control.
Problem Statement
Given a string that contains alphanumeric characters, our task is to move all the digits present in the string to the beginning, while keeping the order of the rest of the characters the same.
Solution Approach
Our approach to solving this problem involves two key steps −
Separate Digits and Non-digits − Traverse the string from left to right, appending all digits to a 'digits' string and all non-digits to a 'nonDigits' string.
Concatenate Strings − Combine the 'digits' and 'nonDigits' strings, ensuring that the digits are at the beginning.
Example
Below are the programs for our problem −
#include <stdio.h> #include <ctype.h> #include <string.h> char* moveDigits(char* str) { char digits[100] = ""; char nonDigits[100] = ""; for (int i = 0; str[i] != '\0'; i++) { if (isdigit(str[i])) strncat(digits, &str[i], 1); else strncat(nonDigits, &str[i], 1); } strcpy(str, digits); strcat(str, nonDigits); return str; } int main() { char str[] = "abc123def456"; char* result = moveDigits(str); printf("String after moving digits to the beginning: %s\n", result); return 0; }
Output
String after moving digits to the beginning: 123456abcdef
#include <bits/stdc++.h> using namespace std; // Function to move all digits to the beginning of a string string moveDigits(string str) { string digits = "", nonDigits = ""; for (char c : str) { if (isdigit(c)) digits += c; else nonDigits += c; } return digits + nonDigits; } int main() { string str = "abc123def456"; string result = moveDigits(str); cout << "String after moving digits to the beginning: " << result << endl; return 0; }
Output
String after moving digits to the beginning: 123456abcdef
public class Main { static String moveDigits(String str) { StringBuilder digits = new StringBuilder(); StringBuilder nonDigits = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (Character.isDigit(c)) digits.append(c); else nonDigits.append(c); } return digits.toString() + nonDigits.toString(); } public static void main(String[] args) { String str = "abc123def456"; String result = moveDigits(str); System.out.println("String after moving digits to the beginning: " + result); } }
Output
String after moving digits to the beginning: 123456abcdef
def move_digits(string): digits = "" non_digits = "" for char in string: if char.isdigit(): digits += char else: non_digits += char return digits + non_digits str = "abc123def456" result = move_digits(str) print("String after moving digits to the beginning:", result)
Output
String after moving digits to the beginning: 123456abcdef
Explanation
Let's consider the string −
str = "abc123def456"
After separating the digits and non-digits, we get:
digits = "123456", nonDigits = "abcdef"
By concatenating these two strings (digits first), we obtain the result:
result = "123456abcdef"
Conclusion
The task of moving all digits to the beginning of a string is a useful exercise for learning string manipulation in various programming languages. It provides valuable insights into how we can traverse and modify strings based on certain conditions.