Page 393 - Computer science 868 Class 12
P. 393

void prn()                                   :  displays the word along with an appropriate message
                        Specify boolean check() and void prn(). Define the main() function to create an object and call the functions accordingly to
                       enable the task. the class NoRepeat, giving details of the constructor(String),

                   Ans.  import java.util.*;
                       class NoRepeat
                       {
                           String word;
                           int len;
                           NoRepeat(String wd)
                           {
                               word=wd;
                               len=0;
                           }

                           boolean check()
                           {
                               int i,j,c;
                               len=word.length();
                               for(i=65;i<=90;i++)
                               {
                                   c=0;
                                   for(j=0;j<len;j++)
                                   {
                                       if(word.charAt(j)==(char)i)
                                       {
                                           c++;
                                       }
                                   }
                                   if(c>1)
                                       return false;
                               }
                               return true;
                           }

                           void display()
                           {
                               if(check())
                                   System.out.println(word +” has no repeated character”);
                               else
                                   System.out.println(word +” has repeated characters”);
                           }

                           public static void main()
                           {
                               Scanner sc= new Scanner(System.in);
                               System.out.println(“Enter a word”);
                               String word=sc.next().toUpperCase();
                               NoRepeat ob = new NoRepeat(word);
                               ob.display();
                           }
                       }
                        // OUTPUT
                        Enter a word
                        INDIA
                        INDIA has repeated characters
                        Enter a word
                        SKY
                        SKY has no repeated character




                                                                                                                       391
                                                                                                              Strings  391
   388   389   390   391   392   393   394   395   396   397   398