Page 404 - Cs_withBlue_J_C11_Flipbook
P. 404

3       {
                4           String s;

                5           int len;
                6           void read() // accept sentence

                7           {
                8               Scanner sc = new Scanner(System.in);

                9               System.out.println("Enter a sentence ending with fullstop");
                10              s = sc.nextLine();
                11              len=s.length();

                12          }

                13
                14          int count(int l)
                15              { if(l==len)  // end of sentence base case

                16                  return 0;
                17            // checking for word seperators


                18               else if(s.charAt(l)==' '||s.charAt(l)==','||s.charAt(l)=='?'
                                ||s.charAt(l)=='.')
                19                   return 1+count(l+1);  // count increase by 1

                20            else
                21                   return count(l+1);

                22              }
                23

                24          void show()
                25          {
                26              int c = count(0);

                27              System.out.println("Number of words = "+c); // display

                28          }
                29
                30          public static void main() // main

                31          {
                32              Word ob = new Word();

                33              ob.read();
                34              ob.show();

                35          }
                36      }




                402402  Touchpad Computer Science-XI
   399   400   401   402   403   404   405   406   407   408   409