How to move all uppercase characters to the beginning of string in Java

1 Answer

0 votes
public class MyClass {
    static public String move_uppercase_to_beginning(String s) { 
        int len = s.length(); 
        String lowercase  = ""; 
        String uppercase = ""; 
   
        char ch; 
        for (int i = 0; i < len; i++) { 
            ch = s.charAt(i); 
   
            if (ch >= 'A' && ch <= 'Z') { 
                uppercase += ch; 
            } 
            else { 
                lowercase += ch; 
            } 
        } 
        return uppercase + lowercase; 
    } 
   
    public static void main(String args[]) {
        String s = "C++ PHP Java"; 
 
        System.out.println(move_uppercase_to_beginning(s)); 
    }
}
 
 
/*
run:
 
CPHPJ++  ava
 
*/

 



answered Aug 20, 2019 by avibootz

Related questions

...