How to move all the capital letters to end of a string in Java

In this context, we will learn how to move all the capital letters to the end of a string in Java.

So if the user enters Let us Learn Java Output will be et us earn avaLLJ.

Java program to move all the capital letters to end of a string

import java.util.*;
public class captialToEnd
 { 
  
    void fetchTheString(String inputstring) 
    {   
  int j;
        int lengthOfstring = inputstring.length(); 
        String alplower = ""; 
        String alpupper = ""; 
        char temp; 
        for (j = 0; j < lengthOfstring; j++) { 
            temp = inputstring.charAt(j);  
            if (temp >= 'A' && temp <= 'Z') { 
                 alpupper+= temp; 
            } 
            else { 
                alplower += temp; 
            } 
        } 
        System.out.println(alplower + alpupper); 
    } 
  
    public static void main(String args[]) 
    { 
        String inputstring;
  Scanner in=new Scanner(System.in);
  System.out.println("Enter the String:");
  inputstring=in.next();
  captialToEnd ce=new captialToEnd();
  ce.fetchTheString(inputstring);
 
    } 
}

OUTPUT:

Enter the String : Java is Object Oriented

ava is bject rientedJOO
  1. The class name capitalToEnd is initialized in which the method fetchTheString is executing our logic.
  2. In the void main function, we are taking inputs from the user.
  3. i)So we are applying the logic like the character is between capital A and capital Z it will be stored in a new string named alpupper
    ii)So if the character will be the lower case so we are storing in alplower .

So the length of the whole sentence is calculated the and that many times the for loop will run.

For each letter it will check it is lowercase or uppercase because we have to check for the whole sentence and will be stored will at the end, we are concatenating the two string of uppercase and lowercase and output is displayed.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *