by Dinesh Thakur Category: array

Algorithm for String Sorting:

step 1: Set two String variables sl,s2, four integer variables count1, count2, size 1, size2 as static

step 2: Read sl,s2

step 3: Set String variable s3=sl.toLowerCase()

step 4: Set String variable s4=s2.toLowerCase()

step 5: Set size1=s3.length()

step 6: Set size2=s4.length()

step 7: Set byte array b1=s3.getBytes()

step 8; Set byte array b2=s4.getBytes()

step 9: Initialize k=97

step 10: repeat through step-30 while (k< 123)

step 11: countl =0

step 12: count2= 0

step 13: Initialize i=0

step 14: repeat through step-16 while (i< size1)

step 15: if b1[i]==k then count1 =count1 +1

step 16: i=i+ 1

step 17: Initialize j=0

step 18: repeat through step-20 while (j< size2)

step 19: if b2[j]==k then count2=count2+ 1

step 20: j=j+ 1

step 21: if countl > count2 then execute following codes else goto step-27

step 22: initialize a=0

step 23: repeat through step-25 while (a< count1)

step 24: print ((char)k)

step 25: a=a+ 1

step 26: goto step- 31

step 27: initialize x=0

step 28: repeat through step-30 while (x< count2)

step 29: print ((char)k)

step 30: x=x+ 1

step 31: Exit

Here is the Java Example for String Sorting:

import java.util.Scanner;

public class StringSorting
                     public static String sl;
                     public static String s2;
                     public static int count1,count2;
                     public static int size1,size2;
                          public static void main(String args[])
                                  Scanner s=new Scanner(;
                                  System.out.print("Enter 1st String :");
                                  System.out.print("Enter 2nd String :");
                                  String s3=sl.toLowerCase();
                                  String s4=s2.toLowerCase();
                                  byte bl[]=s3.getBytes();
                                  byte b2[]=s4.getBytes();
                                  System.out.println(s3+" "+s4);
                                  System.out.println("The Sorted String Output is ");
                                              for(int k=97;k<123;k++)
                                                          for(int i=0;i<size1;i++)
                                                                    for(int j=0;j<size2;j++)
                                                                             if(b2 [j]==k)
                                                                                     for(int a=0;a<count1;a++)
                                                                                             System.out.print((char)k +" ");
                                                                                            for(int x=0;x<count2;x++)
                                                                                                    System.out.print((char)k+" ");

String Sorting in Java

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles