Java Program for Case Specific Sorting


Suppose you have a String that contains uppercase and lowercase characters both. We have to sort this given string in case specific order means if the ith position in the given string had an uppercase letter then the newly sorted string must have uppercase letter at that position and same goes for lowercase letters. Also, both lowercase and uppercase letters will be in sorted order separately.

In this article, we will try to find the solution for the given problem. First, let’s understand the given problem with an example for better understanding.

Example

String st = "KadbHFGtvc";

Assume we have the above string with us. Then, the sorted string would be −

Sorted string: “FabcGHKdtv”

You can see that the first position in the original string had an uppercase letter and also the first letter of new sorted string has an uppercase letter. The 2nd to 4th letters were in lowercase so the new string has lowercase letters too but in a sorted manner. The same goes with other position’s letters too. You can see that both uppercase and lowercase letters are separately sorted.

Case Specific Sorting Program

Let’s talk about the approach we will follow for this problem.

Algorithm

  • Step 1 − Start with creating a class ‘Case’ in which we will define a method named ‘sortCase()’ of void return type along with a parameter ‘st’ of type string.

  • Step 2 − In sortCase() method, we will convert the given string into a character array and store it in ‘chs[]’. Then, we will sort this array using inbuilt method ‘Arrays.sort()’. At this point, in the newly sorted array the uppercase characters starting with ‘A’ to ‘Z’ comes first then lowercase characters ‘a’ to ‘z’.

  • Step 3 − Next, declare and initialize two integer variables ‘caseUp’ and ‘caseLW’ with -1 to store upper and lower case characters respectively.

  • Step 4 − Moving ahead create an if block to check whether first element in array ‘chs[]’ is uppercase letter or not. If so, then we will set variable ‘caseUp’ to 0.

  • Step 5 − Create a while loop that will run till the size of array ‘chs[]’. In this loop, we create another if block to fetch lowercase character and store it in ‘caseLw’.

  • Step 6 − We know that strings are immutable in java therefore we need to create an object ‘new_st’ of StringBuffer class to store newly updated string.

  • Step 7 − Now create a for loop that will run till the size of given string ‘st’. In this loop, we will define an if-else block to compare the positions of uppercase and lowercase characters of string ‘st’ with array ‘chs[]’. Using the if block we will fetch lowercase characters from array ‘chs[]’ and append to ‘new_st’. In the else block we will fetch uppercase characters and append them to ‘new_st’.

  • Step 8 − In the main method, create an object ‘obj’ of class ‘Case’ to call the method ‘sortCase’.

Example

import java.util.*;
public class Case {
   public void sortCase(String st) {
      // converting string into character array
      char chs[] = st.toCharArray(); 
      Arrays.sort(chs); 
      // sorting the character array
      int caseUp = -1;
      int caseLw = -1;
      if(chs[0] <= 'Z') {
         caseUp = 0;
      }
      int i = 0;
      while( i < chs.length) {
         if(chs[i] >= 'Z') {
            caseLw = i;
            break;
         }
         i++;
      }
      StringBuffer new_st = new StringBuffer();
      for(int j = 0; j < st.length(); j++) {
         if(st.charAt(j) >= 'Z') {
            new_st.append(chs[caseLw]);
            caseLw += 1;
         } else {
            new_st.append(chs[caseUp]);
            caseUp += 1;
         }
      }
      System.out.print("The new String after sorting: " + new_st.toString());
   }
   public static void main(String[] args) {
      String st = "KadbHFGtvc";
      System.out.println("The given String: " + st);
      Case obj = new Case(); 
      // Object creation
      obj.sortCase(st); 
      // calling the method using object with an argument
   }
}

Output

The given String: KadbHFGtvc
The new String after sorting: FabcGHKdtv

Conclusion

In this article, we have discussed a java program to solve the given problem. We have used the concept of String, character array and StringBuffer class of java.

Updated on: 05-May-2023

466 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements