Monday, 16 January 2017

Write a Java Program to Sort the String ?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Arrays;

public class StringSorting {

// Method 1 WithOut String API
 public static void withoutStringAPI(String str){
  char[] arr = str.toCharArray();
  
  for(int i=0;i<str.length();i++){
        for(int j=0;j<str.length();j++){
             if(arr[i] <= arr[j]){
                   char temp = arr[i];
                   arr[i] = arr[j];
                   arr[j] = temp;
             }
        }
  }
  System.out.println("METHOD 1 OUTPUT : ");
  for(char c : arr){
        System.out.print(c);
  }
 }

// Method 1 With String API
 public static void withStringAPI(String str){
  char[] arr = str.toCharArray();
  Arrays.sort(arr);
  System.out.println("\n\nMETHOD 2 OUTPUT : ");
  for(char c : arr){
         System.out.print(c);
  }
 }
 public static void main(String[] args) {
         StringSorting.withoutStringAPI("pushkar");
         StringSorting.withStringAPI("bcda");
 }
}

Output :

METHOD 1 OUTPUT : 
ahkprsu

METHOD 2 OUTPUT : 
abcd


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment