Move all Uppercase char to the end of string

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 : ehllo 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program move all uppercase alphabets
// to the end of string
 
#include<bits/stdc++.h>
using namespace std;
 
string move(string str)
{
    // take length of given string
    int len = str.length();
 
    // low store lowercase alphabets
    string low = "";
 
    // upr store uppercase alphabets
    string upr = "";
 
    // traverse string forst char to last char
    char ch;
    for (int i = 0; i < len; i++) {
        ch = str[i] ;
 
        // check char is in uppercase or lower case
        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;
}
// This code is contributed by Ryuga

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program move all uppercase alphabets
// to the end of string
 
public class GFG {
 
    static public String move(String str)
    {
        // take length of given string
        int len = str.length();
 
        // low store lowercase alphabets
        String low = "";
 
        // upr store uppercase alphabets
        String upr = "";
 
        // traverse string forst char to last char
        char ch;
        for (int i = 0; i < len; i++) {
            ch = str.charAt(i);
 
            // check char is in uppercase or lower case
            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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program move all uppercase
# alphabets to the end of string
 
def move(str):
     
    # take length of given string
    len__ = len(str)
 
    # low store lowercase alphabets
    low = ""
 
    # upr store uppercase alphabets
    upr = ""
 
    # traverse string forst char to last char
    for i in range(0, len__, 1):
        ch = str[i]
 
        # check char is in uppercase or
        # lower case
        if (ch >= 'A' and ch <= 'Z'):
            upr += ch
     
        else:
            low += ch
         
    return low + upr
         
# Driver Code
if __name__ == '__main__':
    str = "Geeksforgeeks A Computer Science Portal for Geeks!!"
    print("Before Operation:", str)
    print("After Operation:", move(str))
 
# This code is contributed by
# Sahil_Shelangia

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program move all uppercase
// alphabets to the end of string
using System;
 
class GFG
{
 
static public string move(string str)
{
    // take length of given string
    int len = str.Length;
 
    // low store lowercase alphabets
    string low = "";
 
    // upr store uppercase alphabets
    string upr = "";
 
    // traverse string forst char
    // to last char
    char ch;
    for (int i = 0; i < len; i++)
    {
        ch = str[i];
 
        // check char is in uppercase
        // or lower case
        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));
}
}
 
// This code is contributed
// by Mukul Singh

chevron_right


Output: 



Before Operation:   Geeksforgeeks A Computer Science Portal for Geeks!!
After Operation:    eeksforgeeks  omputer cience ortal for eeks!!GACSPG

Method #2: Using Regular Expression 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program move all uppercase alphabets to
// the end of string
public class GFG {
 
    // Function return a sting with all
    // uppercase leter to the end of string
    static public String move(String s)
    {
        // first take all lower case leter
        // and take all uppercase letter
        // and Finally concatenate both and return
        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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program move all uppercase alphabets
# to the end of string
import  re
 
# Function return a sting with all
# uppercase leter to the end of string
def move(s):
     
    # First take all lower case leter
    # and take all uppercase letter
    # and Finally concatenate both and return
    words = re.findall('[a-z]*', s)
    words1 = re.findall('[A-Z]*', s)
    words2 = re.findall('[@_!#$%^&*()<>?/|}{~:]', s)
 
    return (' '.join(words) + ''.join(words2) +
             ''.join(words1))
 
# Driver code
if __name__ == '__main__':
 
    str = "Geeksforgeeks A Computer " \
          "Science Portal for Geeks!!"
 
    print("Befour Operation:   " + str)
    print("After Operation:    " + move(str))
 
# This code is contributed by gauravrajput1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program move all uppercase
// alphabets to the end of string
using System;
using System.Text.RegularExpressions;
class GFG{
 
// Function return a sting with
// all uppercase leter to the
// end of string
static public String move(String s)
{
  // first take all lower case
  // leter and take all uppercase
  // letter and Finally concatenate
  // both and return
  var reg = new Regex(@"[A-Z]");
  var reg1 = new Regex(@"[^A-Z]");
  return reg.Replace(s, "") +
         reg1.Replace(s, "") ;
}
 
// Driver code
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));
}
}
 
// This code is contributed by Rajput-Ji

chevron_right


Output:  

Before Operation:   Geeksforgeeks A Computer Science Portal for Geeks!!
After Operation:    eeksforgeeks  omputer cience ortal for eeks!!GACSPG

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Just another competitive programmer and hard worker

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.