Print the characters in descending order Program in java

Question:

Write a program to get the String an input from the user and display the alphabets in the String in descending order (Assume all the characters are given in lower case).

Note : In the String “programming” the characters m or r or g is repeated

Sample Input 1:

love

Sample Output 1:

vole

Sample Input 2:

programming

Sample Output 2:

rponmiga

CODE:

import java.util.*;
public class Main 
{
    public static void main (String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        String input=sc.next();
        String[] arr=input.split("");
        Arrays.sort(arr, Collections.reverseOrder());
        for(int i=0;i<arr.length;i++)
        {
            if(i==0)
            {
                System.out.print(arr[i]);
            }
            else if(arr[i].equals(arr[i-1]))
            {
                continue;
            }
            else
            {
                System.out.print(arr[i]);
            }
        }
    }
}
Next:
  1. Vowels in a fishBowl
  2. Least offers
  3. Ascending and descending order

Leave a Comment

Scroll to Top