Page 277 - Cs_withBlue_J_C11_Flipbook
P. 277

Choice 1:                 Choice 2:
                        I                         A
                        IN                        AI
                        IND                       AID
                        INDI                      AIDN
                        INDIA                     AIDNI
                   Ans.  import java.util.*;
                       class pattern_choice
                       {
                           public static void main()
                           {
                               Scanner sc= new Scanner(System.in);
                               String wd, extwd;
                               int i, l, j, ch;
                               System.out.print("Enter a word: ");
                               wd=sc.next();
                               wd=wd.toUpperCase();
                               l=wd.length();
                               System.out.println("Enter 1 for pattern 1 / 2 for pattern 2 ");
                               ch=sc.nextInt();
                               switch(ch)
                               {
                               case 1:
                                   for(i=0; i<l; i++)
                                   {
                                       extwd=wd.substring(0,i+1);
                                       System.out.println(extwd);
                                   }
                                   break;
                                case 2:
                                   for(i=l-1; i>=0; i--)
                                   {
                                       extwd="";
                                       for(j=i; j<l; j++)
                                       {
                                           extwd=extwd+wd.charAt(j);
                                       }
                                       System.out.println(extwd);
                                   }
                                   break;
                                   default: System.out.println("Wrong choice");
                               }
                           }
                       }


                      Unsolved Questions


                 A.  Tick ( ) the correct answer:
                    1.  Which of the following methods is used to join the strings together?
                       a.  concat()                                    b.  append()
                       c.  concatenate()                               d.  Both a and b




                                                                                                                       275
                                                                                                              Strings  275
   272   273   274   275   276   277   278   279   280   281   282