Page 397 - Computer science 868 Class 12
P. 397

Ans.  import java.util.*;
                       class Capital
                       {
                        String sent;
                        int freq;
                        Capital()
                        {
                        sent="";
                        freq=0;
                        }
                        void input()
                        {
                        Scanner sc=new Scanner(System.in);
                        System.out.println("Enter the sentence");
                        sent=sc.nextLine();
                        }
                        boolean isCap(String w)
                        {
                        char c=w.charAt(0);
                        if (Character.isUpperCase(c) )
                        return true;
                        else
                        return false;
                        }
                        void display()
                        { System.out.println("sentence="+sent);
                        StringTokenizer ob=new StringTokenizer(sent);
                        int count=ob.countTokens();
                        for (int i=0;i<count;i++)
                        { String wd=ob.nextToken();
                        if (isCap(wd))
                        freq=freq+1;
                        }
                        System.out.println("Freqency of title word="+freq);
                        }
                        public static void main(String[] args)
                        {
                        Capital ob=new Capital();
                        ob.input();
                        ob.display();
                        }
                        }
                    6.  A class SwapSort has been defined to perform string related operations on a word input. Some of the members of the class are
                       as follows:                                                                               [ISC 2017]
                        Class name                                :   SwapSort
                        Data Members/Instance variables
                        wrd                                       :   to store a word
                        len                                       :  integer to store the length of the word
                        swapwrd                                   :   to store the swapped word
                        sortwrd                                   :   to store the sorted word
                        Member Functions/Methods
                        SwapSort()                                :    default constructor to initialize data members with legal initial
                                                                    values
                        void readword()                           :   to accept a word in UPPER CASE
                        void swapchar()                           :    to interchange/swap the first and last characters of the word in
                                                                    ‘wrd’ and stores the new word in ‘swapwrd’



                                                                                                                       395
                                                                                                              Strings  395
   392   393   394   395   396   397   398   399   400   401   402