Given a string, move all Uppercase alphabets char to the end of the String.
Examples:
Input : Geeksforgeeks A Computer Science Portal for Geeks!!
Output : eeksforgeeks omputer cience ortal for eeks!!GACSPG
Input : Hello India
Output : ello ndiaHI
Method #1: Without Using Regular Expression
The idea is to traverse the input string and maintain two strings, one string that contains lowercase characters (a, c, z, etc) and the other string that maintains Uppercase characters (A, C, Z, etc). Finally, concatenate the two strings and return.
Below is the implementation.
C++
#include<bits/stdc++.h>
using namespace std;
string move(string str)
{
int len = str.length();
string low = "" ;
string upr = "" ;
char ch;
for ( int i = 0; i < len; i++) {
ch = str[i] ;
if (ch >= 'A' && ch <= 'Z' ) {
upr += ch;
}
else {
low += ch;
}
}
return low + upr;
}
int main()
{
string str = "Geeksforgeeks A Computer Science Portal for Geeks!!" ;
cout << "Before Operation: " << str << endl;
cout << "After Operation: " << move(str) << endl;
return 0;
}
|
Java
public class GFG {
static public String move(String str)
{
int len = str.length();
String low = "" ;
String upr = "" ;
char ch;
for ( int i = 0 ; i < len; i++) {
ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z' ) {
upr += ch;
}
else {
low += ch;
}
}
return low + upr;
}
public static void main(String args[])
{
String str = "Geeksforgeeks A Computer Science Portal for Geeks!!" ;
System.out.println( "Before Operation: " + str);
System.out.println( "After Operation: " + move(str));
}
}
|
Python3
def move( str ):
len__ = len ( str )
low = ""
upr = ""
for i in range ( 0 , len__, 1 ):
ch = str [i]
if (ch > = 'A' and ch < = 'Z' ):
upr + = ch
else :
low + = ch
return low + upr
if __name__ = = '__main__' :
str = "Geeksforgeeks A Computer Science Portal for Geeks!!"
print ( "Before Operation:" , str )
print ( "After Operation:" , move( str ))
|
C#
using System;
class GFG
{
static public string move( string str)
{
int len = str.Length;
string low = "" ;
string upr = "" ;
char ch;
for ( int i = 0; i < len; i++)
{
ch = str[i];
if (ch >= 'A' && ch <= 'Z' )
{
upr += ch;
}
else
{
low += ch;
}
}
return low + upr;
}
public static void Main()
{
string str = "Geeksforgeeks A Computer Science Portal for Geeks!!" ;
Console.WriteLine( "Before Operation: " + str);
Console.WriteLine( "After Operation: " + move(str));
}
}
|
Javascript
function move(str) {
const len = str.length;
let low = "" ;
let upr = "" ;
let ch;
for (let i = 0; i < len; i++) {
ch = str.charAt(i);
if (ch >= "A" && ch <= "Z" ) {
upr += ch;
} else {
low += ch;
}
}
return low + upr;
}
const str = "Geeksforgeeks A Computer Science Portal for Geeks!!" ;
console.log( "Before Operation: " + str);
console.log( "After Operation: " + move(str));
|
Output
Before Operation: Geeksforgeeks A Computer Science Portal for Geeks!!
After Operation: eeksforgeeks omputer cience ortal for eeks!!GACSPG
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
Method #2: Using Regular Expression
C++
#include <bits/stdc++.h>
using namespace std;
string move(string s)
{
regex re( "[A-Z]+" );
regex re1( "[^A-Z]+" );
return regex_replace(s, re, "" ) + regex_replace(s, re1, "" );
}
int main()
{
string str = "Geeksforgeeks A Computer Science Portal for Geeks!!" ;
cout << "Befour Operation: " << str << endl;
cout << "After Operation: " << move(str);
}
|
Java
public class GFG {
static public String move(String s)
{
return s.replaceAll( "[A-Z]+" , "" ) + s.replaceAll( "[^A-Z]+" , "" );
}
public static void main(String args[])
{
String str = "Geeksforgeeks A Computer Science Portal for Geeks!!" ;
System.out.println( "Befour Operation: " + str);
System.out.println( "After Operation: " + move(str));
}
}
|
Python3
import re
def move(s):
words = re.findall( '[a-z]*' , s)
words1 = re.findall( '[A-Z]*' , s)
words2 = re.findall( '[@_!#$%^&*()<>?/|}{~:]' , s)
return ( ' ' .join(words) + ''.join(words2) +
''.join(words1))
if __name__ = = '__main__' :
str = "Geeksforgeeks A Computer " \
"Science Portal for Geeks!!"
print ( "Befour Operation: " + str )
print ( "After Operation: " + move( str ))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG{
static public String move(String s)
{
var reg = new Regex( @"[A-Z]" );
var reg1 = new Regex( @"[^A-Z]" );
return reg.Replace(s, "" ) +
reg1.Replace(s, "" ) ;
}
public static void Main(String []args)
{
String str = "Geeksforgeeks A Computer" +
"Science Portal for Geeks!!" ;
Console.WriteLine( "Befour Operation: " +
str);
Console.WriteLine( "After Operation: " +
move(str));
}
}
|
Javascript
<script>
function move(s)
{
return s.replace(/[A-Z]/g, "" );
}
var str = "Geeksforgeeks A Computer Science Portal for Geeks!!" ;
document.write( "Befour Operation: " + str + "<br>" );
document.write( "After Operation: " + move(str));
</script>
|
Output
Befour Operation: Geeksforgeeks A Computer Science Portal for Geeks!!
After Operation: eeksforgeeks omputer cience ortal for eeks!!GACSPG
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)