Showing posts with label Write a program which sort the names Alphabetically in a given string. Show all posts
Showing posts with label Write a program which sort the names Alphabetically in a given string. Show all posts

Friday, 12 September 2014

Name Sorting in String

Structure of the Problem Requirements 

In this problem we will learn how to sort a string alphabetically. Just like Array sorting we also sort it with Bubble sort simple logic. A temporary string is used to rotate the alphabet and save them in array. Here is the source code of this problem which help you in better understanding.

Source Code



class SortString
{
               public static void main(String args[])
                  {
                     Stringstrings[]={"Naeem","Asad","Usman","Facebook","Twitter"};
                     String sort;
                     for(int x=0;x<strings.length;x++)
                       {
                          for(int y=x+1;y<strings.length;y++)
                               {
                            if(strings[x].compareTo(strings[y])>0)
                                     {
                                         sort=strings[x];
                                         strings[x]=strings[y];
                                         strings[y]=sort;
                                      }
                               }
                         }
                          System.out.println("Sorted Array of Strings:");
                          for(int z=0;z<strings.length;z++)
                                    System.out.println(strings[z]);
                    }
}

Output of the Program