Move all special char to the end of the String - GeeksforGeeks

07 May, 2021
In this article, we will learn how to move all special char to the end of the String.Examples:  
Input : !@$%^&*AJAY Output :AJAY!@$%^&*Input :Geeksf!@orgeek@s A#$ c%o^mputer s****cience p#o@rtal fo@r ge%eks Output :Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
 
Prerequisite : Regular Expressions in JavaThe idea is to traverse input string and maintain two strings, one string that contains normal characters (a, A, 1, ‘ ‘, etc) and other string that maintains special characters (@, $, etc). Finally, concatenate the two strings and return.Here is the implementation of above approach  
C++
Java
Python3
C#
Javascript
// C++ program move all special char to the end of the string #include <bits/stdc++.h>using namespace std; // Function return a string with all special// chars to the endstring moveAllSC(string str){    // Take length of string    int len = str.length();     // traverse string    string res1 = "", res2 = "";    for (int i = 0; i < len; i++)    {        char c = str.at(i);         // check char at index i is a special char        if (isalnum(c) || c == ' ')            res1 += c;        else            res2 += c;    }     return res1 + res2;} // Driver codeint main(){    string str1("Geeksf!@orgeek@s A#$ c%o^mputer");    string str2(" s****cience p#o@rtal fo@r ge%eks");    string str = str1 + str2;    cout << moveAllSC(str) << endl;    return 0;} // This code is contributed by// sanjeev2552
// Java program move all special char to the end of the stringclass GFG1 {     // Function return a string with all special    // chars to the end    static String moveAllSC(String str)    {        // Take length of string        int len = str.length();         // regular expression for check char is special        // or not.        String regx = "[a-zA-Z0-9\\s+]";         // traverse string        String res1 = "", res2 = "";        for (int i = 0; i < len; i++) {                     char c = str.charAt(i);             // check char at index i is a special char            if (String.valueOf(c).matches(regx))               res1 = res1 + c;            else               res2 = res2 + c;        }        return res1 + res2;    }     public static void main(String args[])    {        String str = "Geeksf!@orgeek@s A#$ c%o^mputer"                     + " s****cience p#o@rtal fo@r ge%eks";        System.out.println(moveAllSC(str));    }}
# Python3 program move all special char# to the end of the string # Function return a string with all# special chars to the enddef moveAllSC(string):         # Take length of string    length = len(string)     # Traverse string    res1, res2 = "", ""    for i in range(0, length):                 c = string[i]         # check char at index i is a special char        if c.isalnum() or c == " ":            res1 = res1 + c        else:            res2 = res2 + c             return res1 + res2     # Driver Codeif __name__ == "__main__":         string = "Geeksf!@orgeek@s A#$ c%o^mputer" \            +" s****cience p#o@rtal fo@r ge%eks"         print(moveAllSC(string))     # This code is contributed by Rituraj Jain
// C# program move all special char// to the end of the stringusing System;using System.Text.RegularExpressions; class GFG{     // Function return a string with all    // special chars to the end    static String moveAllSC(String str)    {        // Take length of string        int len = str.Length;         // regular expression to check        // char is special or not.        var regx = new Regex("[a-zA-Z0-9\\s+]");         // traverse string        String res1 = "", res2 = "";        for (int i = 0; i < len; i++)        {            char c = str[i];             // check char at index i is a special char            if (regx.IsMatch(c.ToString()))                res1 = res1 + c;            else                res2 = res2 + c;        }        return res1 + res2;    }     public static void Main(String []args)    {        String str = "Geeksf!@orgeek@s A#$ c%o^mputer" +                     " s****cience p#o@rtal fo@r ge%eks";        Console.WriteLine(moveAllSC(str));    }} // This code is contributed by PrinciRaj1992
<script>      // JavaScript program move all special char      // to the end of the string      // Function return a string with all      // special chars to the end      function moveAllSC(str) {        // Take length of string        var len = str.length;         // regular expression to check        // char is special or not.        var regx = new RegExp("[a-zA-Z0-9\\s+]");         // traverse string        var res1 = "",          res2 = "";        for (var i = 0; i < len; i++) {          var c = str[i].toString();          // check char at index i is a special char          if (regx.test(c)) res1 = res1 + c;          else res2 = res2 + c;        }        return res1 + res2;      }       var str =        "Geeksf!@orgeek@s A#$ c%o^mputer" + " s****cience p#o@rtal fo@r ge%eks";      document.write(moveAllSC(str));    </script>
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
 
rituraj_jain
sanjeev2552
princiraj1992
rdtank
java-regular-expression
Java-Strings
Technical Scripter 2018
Java Programs
Strings
Technical Scripter
Java-Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Iterate HashMap in Java?
Iterate Over the Characters of a String in Java
How to Get Elements By Index from HashSet in Java?
Java Program to Write into a File
Modulo or Remainder Operator in Java
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types