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:-
- Array square
- Generate number using odd digits
- Alternate Number Difference
- Next Greatest number
- Mark Comparison
- Print the characters in descending order
- Vowels in a fishBowl
- Least offer
- Ascending and descending order
- Mail Domain
- Count repeating words
- Sentence – Convert to upper and lower
- Count consecutive repeating characters
- Zig zag Array
- Pass and Fail Count
- Search a Course
- Average and Grade Calculation
- String – Find and replace the character (first occurrence)
- Sort the first and second half of an array
- Retail Shop
- Palindrome
- Numerology
- InitCap
- Array Compatiblilty
- Sum of the maximum and the minimum element
- String Concatenation
- Find Average Age
- Login