Page 427 - CA_Blue( J )_Class10
P. 427

6          Scanner sc= new Scanner(System.in);
                   7          String str,newstr="";

                   8          int i,l,cw=0;
                   9          char ch,ch1;

                  10          System.out.print("Enter a sentence : ");
                  11          str=sc.nextLine();

                  12          l=str.length();
                  13          for(i=0;i<l;i++)

                  14          {
                  15              ch=str.charAt(i);

                  16              if(ch>='A' && ch<='Z')
                  17                  ch1 = (char)(ch+32);
                  18              else

                  19              if(ch>='a' && ch<='z')

                  20                  ch1 = (char)(ch-32);
                  21              else
                  22                  ch1=ch;

                  23              newstr=newstr+ch1;
                  24          }

                  25          System.out.println("Old String    : "+str);
                  26          System.out.println("Converted string: "+newstr);

                  27      }
                  28  }


                               To input a sentence. Also input a character and replace it with another character taken from user.
                  Program 3
                               The program is case-sensitive.

                   1  import java.util.*;

                   2  class replace_char
                   3  {

                   4      public static void main()
                   5      {

                   6          Scanner sc= new Scanner(System.in);
                   7          String str, newstr ="";

                   8          int i, l;
                   9          char ch, chr, chi;



                                                                                                                       425
                                                                                                       String Handling  425
   422   423   424   425   426   427   428   429   430   431   432