Java Program of Print the characters in descending order

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

Main.java

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]);
            }
        }
    }
}

Recommended:-

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

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

x