In this article, we will learn how to move all special char to the end of the String.
Input : !@$%^&*AJAY
Output :AJAY!@$%^&*
Input :Beginnerf!@orgeek@s A#$ c%o^mputer s****cience p#o@rtal fo@r ge%eks
Output :w3wiki A computer science portal for Beginner!@@#$%^****#@@%
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
The idea is to traverse the input string and maintain two strings, one string that contains normal characters (a, A, 1, ‘ ‘, etc) and another string that maintains special characters (@, $, etc). Finally, concatenate the two strings and return.
C++
#include <bits/stdc++.h>
using namespace std;
string moveAllSC(string str)
{
int len = str.length();
string res1 = "" , res2 = "" ;
for ( int i = 0; i < len; i++)
{
char c = str.at(i);
if ( isalnum (c) || c == ' ' )
res1 += c;
else
res2 += c;
}
return res1 + res2;
}
int main()
{
string str1( "Beginnerf!@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;
}
|
Java
class GFG1 {
static String moveAllSC(String str)
{
int len = str.length();
String regx = "[a-zA-Z0-9\\s+]" ;
String res1 = "" , res2 = "" ;
for ( int i = 0 ; i < len; i++) {
char c = str.charAt(i);
if (String.valueOf(c).matches(regx))
res1 = res1 + c;
else
res2 = res2 + c;
}
return res1 + res2;
}
public static void main(String args[])
{
String str = "Beginnerf!@orgeek@s A#$ c%o^mputer"
+ " s****cience p#o@rtal fo@r ge%eks" ;
System.out.println(moveAllSC(str));
}
}
|
Python3
def moveAllSC(string):
length = len (string)
res1, res2 = " ", " "
for i in range ( 0 , length):
c = string[i]
if c.isalnum() or c = = " " :
res1 = res1 + c
else :
res2 = res2 + c
return res1 + res2
if __name__ = = "__main__" :
string = "Beginnerf!@orgeek@s A#$ c%o^mputer" \
+ " s****cience p#o@rtal fo@r ge%eks"
print (moveAllSC(string))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static String moveAllSC(String str)
{
int len = str.Length;
var regx = new Regex( "[a-zA-Z0-9\\s+]" );
String res1 = "" , res2 = "" ;
for ( int i = 0; i < len; i++)
{
char c = str[i];
if (regx.IsMatch(c.ToString()))
res1 = res1 + c;
else
res2 = res2 + c;
}
return res1 + res2;
}
public static void Main(String []args)
{
String str = "Beginnerf!@orgeek@s A#$ c%o^mputer" +
" s****cience p#o@rtal fo@r ge%eks" ;
Console.WriteLine(moveAllSC(str));
}
}
|
Javascript
<script>
function moveAllSC(str) {
var len = str.length;
var regx = new RegExp( "[a-zA-Z0-9\\s+]" );
var res1 = "" ,
res2 = "" ;
for ( var i = 0; i < len; i++) {
var c = str[i].toString();
if (regx.test(c)) res1 = res1 + c;
else res2 = res2 + c;
}
return res1 + res2;
}
var str =
"Beginnerf!@orgeek@s A#$ c%o^mputer" + " s****cience p#o@rtal fo@r ge%eks" ;
document.write(moveAllSC(str));
</script>
|
Output
w3wiki A computer science portal for Beginner!@@#$%^****#@@%
- Time Complexity: O(N), as we use a loop to traverse N times. Where N is the length of the string.
- Auxiliary Space: O(N), as we use extra space for the strings res1 and res2. Where N is the length of the string.
- One pointer will start from the beginning of the string and the other pointer will start from the end of the string.
- The first pointer will move forward until it encounters a special character, while the second pointer will move backward until it encounters a non-special character.
- Then, the two characters at the two pointers will be swapped.
- This process will continue until the two pointers meet in the middle.
C++
#include <iostream>
using namespace std;
string moveSpecialCharToEnd(string s)
{
string result = "" ;
string specialChar = "" ;
for ( int i = 0; i < s.length(); i++) {
if ((s[i] >= 'A' && s[i] <= 'Z' )
|| (s[i] >= 'a' && s[i] <= 'z' )
|| (s[i] >= '0' && s[i] <= '9' )
|| s[i] == ' ' ) {
result += s[i];
}
else {
specialChar += s[i];
}
}
return result + specialChar;
}
int main()
{
string str = "Beginnerf!@orgeek@s A#$ c%o^mputer "
"s****cience p#o@rtal fo@r ge%eks" ;
cout << moveSpecialCharToEnd(str) << endl;
return 0;
}
|
Java
public class Main {
public static String moveSpecialCharToEnd(String s)
{
String result = "" ;
String specialChar = "" ;
for ( int i = 0 ; i < s.length(); i++) {
if ((s.charAt(i) >= 'A' && s.charAt(i) <= 'Z' )
|| (s.charAt(i) >= 'a'
&& s.charAt(i) <= 'z' )
|| (s.charAt(i) >= '0'
&& s.charAt(i) <= '9' )
|| s.charAt(i) == ' ' ) {
result += s.charAt(i);
}
else {
specialChar += s.charAt(i);
}
}
return result + specialChar;
}
public static void main(String[] args)
{
String str = "Beginnerf!@orgeek@s A#$ c%o^mputer "
+ "s****cience p#o@rtal fo@r ge%eks" ;
System.out.println(moveSpecialCharToEnd(str));
}
}
|
Python3
def moveSpecialCharToEnd(s):
result = ""
specialChar = ""
for i in range ( len (s)):
if ((s[i] > = 'A' and s[i] < = 'Z' )
or (s[i] > = 'a' and s[i] < = 'z' )
or (s[i] > = '0' and s[i] < = '9' )
or s[i] = = ' ' ):
result + = s[i]
else :
specialChar + = s[i]
return result + specialChar
str = "Beginnerf!@orgeek@s A#$ c%o^mputer " \
"s****cience p#o@rtal fo@r ge%eks"
print (moveSpecialCharToEnd( str ))
|
C#
using System;
public class GFG
{
public static string MoveSpecialCharToEnd( string s)
{
string result = "" ;
string specialChar = "" ;
for ( int i = 0; i < s.Length; i++)
{
if ((s[i] >= 'A' && s[i] <= 'Z' )
|| (s[i] >= 'a' && s[i] <= 'z' )
|| (s[i] >= '0' && s[i] <= '9' )
|| s[i] == ' ' )
{
result += s[i];
}
else
{
specialChar += s[i];
}
}
return result + specialChar;
}
public static void Main()
{
string str = "Beginnerf!@orgeek@s A#$ c%o^mputer "
+ "s****cience p#o@rtal fo@r ge%eks" ;
Console.WriteLine(MoveSpecialCharToEnd(str));
}
}
|
Javascript
function moveSpecialCharToEnd(s) {
let result = "" ;
let specialChar = "" ;
for (let i = 0; i < s.length; i++) {
if (
(s[i] >= 'A' && s[i] <= 'Z' ) ||
(s[i] >= 'a' && s[i] <= 'z' ) ||
(s[i] >= '0' && s[i] <= '9' ) ||
s[i] === ' '
) {
result += s[i];
} else {
specialChar += s[i];
}
}
return result + specialChar;
}
function main() {
const str =
"Beginnerf!@orgeek@s A#$ c%o^mputer " +
"s****cience p#o@rtal fo@r ge%eks" ;
const result = moveSpecialCharToEnd(str);
console.log(result);
}
main();
|
Output
w3wiki A computer science portal for Beginner!@@#$%^****#@@%
Time Complexity: O(n), where n is the length of the input string, as the code is traversing the string only once.
Auxiliary Space: O(n), as two new strings are created to store the characters: one for normal characters and another for special characters.
Approach 3 – Using ‘isalnum()’ :
- Start with an empty string ‘special_chars’ to store the special characters and an empty string ‘normal_chars’ to store the normal characters.
- Iterate through each character ‘char’ in the input string.
- For each character: a. Check if char is alphanumeric or a space using the isalnum() method or if it is a whitespace character using the isspace() method. b. If char is alphanumeric or a space, append it to the normal_chars string. c. If char is a special character, append it to the special_chars string.
- After iterating through all characters, return the concatenation of normal_chars and special_chars.
- End of the function.
Bellow is the implementation of the approach .
C++
#include <iostream>
#include <string>
std::string moveAllSpecialChars( const std::string& input)
{
std::string specialChars = "" ;
std::string normalChars = "" ;
for ( char character : input) {
if ( isalnum (character) || isspace (character)) {
normalChars += character;
}
else {
specialChars += character;
}
}
return normalChars + specialChars;
}
int main()
{
std::string input = "Beginnerf!@orgeek@s A#$ c%o^mputer "
"s****cience p#o@rtal fo@r ge%eks" ;
std::string result = moveAllSpecialChars(input);
std::cout << result << std::endl;
return 0;
}
|
Java
public class Main {
public static String moveAllSpecialChars(String input)
{
String specialChars = "" ;
String normalChars = "" ;
for ( char character : input.toCharArray()) {
if (Character.isLetterOrDigit(character)
|| Character.isWhitespace(character)) {
normalChars += character;
}
else {
specialChars += character;
}
}
return normalChars + specialChars;
}
public static void main(String[] args)
{
String input = "Beginnerf!@orgeek@s A#$ c%o^mputer "
+ "s****cience p#o@rtal fo@r ge%eks" ;
String result = moveAllSpecialChars(input);
System.out.println(result);
}
}
|
Python
def move_all_special_chars(string):
special_chars = ''
normal_chars = ''
for char in string:
if char.isalnum() or char.isspace():
normal_chars + = char
else :
special_chars + = char
return normal_chars + special_chars
string = "Beginnerf!@orgeek@s A#$ c%o^mputer s****cience p#o@rtal fo@r ge%eks"
result = move_all_special_chars(string)
print (result)
|
C#
using System;
using System.Text;
class Program {
static string MoveAllSpecialChars( string input)
{
string specialChars = "" ;
string normalChars = "" ;
foreach ( char character in input)
{
if ( char .IsLetterOrDigit(character)
|| char .IsWhiteSpace(character)) {
normalChars += character;
}
else {
specialChars += character;
}
}
return normalChars + specialChars;
}
static void Main()
{
string input = "Beginnerf!@orgeek@s A#$ c%o^mputer "
+ "s****cience p#o@rtal fo@r ge%eks" ;
string result = MoveAllSpecialChars(input);
Console.WriteLine(result);
}
}
|
Javascript
function moveAllSpecialChars(input) {
let specialChars = "" ;
let normalChars = "" ;
for (let character of input) {
if (/[a-zA-Z0-9\s]/.test(character)) {
normalChars += character;
} else {
specialChars += character;
}
}
return normalChars + specialChars;
}
let input = "Beginnerf!@orgeek@s A#$ c%o^mputer s****cience p#o@rtal fo@r ge%eks" ;
let result = moveAllSpecialChars(input);
console.log(result);
|
Output
w3wiki A computer science portal for Beginner!@@#$%^****#@@%
Time Complexity: O(n)
Auxiliary Space: O(n)