Computer Science, asked by sakshimish2345, 19 days ago

Write a program to input a string and print it in sorted form.

Example-
“dcab”
Output- “abcd”​

Answers

Answered by samarthkrv
0

Answer:

import java.util.*;

public class Main

{

   static String sort(String s){

       int n = s.length();

       char[] letters = new char[n];

       String sorted = "";

           for(int i = 0; i < n; i++){

               letters[i] = s.charAt(i);

           }

           Arrays.sort(letters);

           for (char c : letters){

               sorted = sorted + c;

           }

           return sorted;

   }

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

 System.out.print("Enter a string:");

 String inp = sc.nextLine();

 System.out.println("Original string: " + inp);

 System.out.println("Sorted string: " + sort(inp));

}

}

Explanation:

Similar questions