Page 275 - Cs_withBlue_J_C11_Flipbook
P. 275

}
                           }
                           void display()
                           {
                               System.out.println("Original word: "+word);
                               System.out.println("Sorted word: "+sortwd);
                           }

                           public static void main()
                           {
                               convert ob= new convert();
                               ob.input();
                               ob.sortword();
                               ob.display();
                           }
                       }
                    5.  Create a class Remove_Duplicates to remove duplicate characters from the given word. For example:
                        Sample Input:  Applications
                        Sample Output:  Aplictons
                        Class name             :   Remove_Duplicates
                        Data Members
                        String wd              :   To input the word
                        tempwd                 :   To hold the value of "wd" variable
                        Member Methods
                        Remove_Duplicates(String ts)   :   parameterized constructor
                        void dup_rev()         :   removes the duplicate characters from the word
                        void display()         :   prints both the original word and the new word

                   Ans.  import java.util.*;
                       class Remove_Duplicates
                       {
                           String wd,tempwd;
                           Remove_Duplicates(String ts)
                           {
                               wd=ts;
                               tempwd=wd;
                           }
                           void dup_rev()
                           {
                               int l=wd.length();
                               int c, i;
                               String s1="";
                               for( i=0; i<(l-1); i++)
                               {
                                   s1=wd.substring(0,i+1);
                                   c=0;
                                   for(int j=i+1; j<l; j++)
                                   {
                                       if(wd.charAt(i)==wd.charAt(j))
                                       {
                                           c++;
                                           continue;
                                       }
                                       else
                                           s1=s1+wd.charAt(j);
                                   }
                                   wd=s1;


                                                                                                                       273
                                                                                                              Strings  273
   270   271   272   273   274   275   276   277   278   279   280