Move all special char to the end of the String
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 Java
The 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++
// 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 end string 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 code int 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
// Java program move all special char to the end of the string class 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
# Python3 program move all special char # to the end of the string # Function return a string with all # special chars to the end def 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 Code if __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#
// C# program move all special char // to the end of the string using 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 |
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
Recommended Posts:
- Move all Uppercase char to the end of string
- Count special palindromes in a String
- Reverse a string without affecting special characters
- Check if a string can be rearranged to form special palindrome
- Split numeric, alphabetic and special symbols from a String
- Move spaces to front of string in single traversal
- Program to count vowels, consonant, digits and special characters in string.
- Python code to move spaces to front of string in single traversal
- Convert string to char array in C++
- Sudo Placement | Special Subsequences
- Sum of special triplets having elements from 3 arrays
- Number of special pairs possible from the given two numbers
- Count the number of special permutations
- Inverting the Move to Front Transform
- Find Largest Special Prime which is less than or equal to a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.