Page 429 - CA_Blue( J )_Class10
P. 429

14         sp=str.lastIndexOf(' ');

                  15         for(i=0;i<sp;i++)
                  16         {

                  17             ch=str.charAt(i);
                  18             if(ch==' ')

                  19                 newstr=newstr+str.charAt(i+1)+ ".";
                  20         }
                  21         newstr=newstr+str.substring(sp+1);

                  22         System.out.println("Original Name : "+str);

                  23         System.out.println("Abbreviated Name : "+newstr);
                  24     }
                  25 }



                  Program 5      To input a sentence and a word. Count the number of times the word is in the sentence.



                   1 import java.util.*;
                   2 class count_word
                   3 {

                   4     public static void main()

                   5     {
                   6         Scanner sc= new Scanner(System.in);

                   7         String str, wd, extwd="";
                   8         int i, l,c=0;

                   9         char ch;
                  10         System.out.print("Enter a sentence: ");

                  11         str=sc.nextLine();
                  12         str= str+ " " ;

                  13         System.out.print("Enter word to count: ");
                  14         wd=sc.next ();
                  15         l=str.length();

                  16         for(i=0;i<l;i++)

                  17         {
                  18             ch=str.charAt(i);
                  19             if(ch!=' ')

                  20                 extwd=extwd+ch;


                                                                                                                       427
                                                                                                       String Handling  427
   424   425   426   427   428   429   430   431   432   433   434