Advertisement
Hydrase

String Manipulation (Java)

Jul 8th, 2025 (edited)
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2. class strman
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         String s1,s2;
  7.         Scanner sc=new Scanner(System.in);
  8.         int ch;
  9.         System.out.println("Enter 2 strings: ");
  10.         s1=sc.next();
  11.         s2=sc.next();
  12.         System.out.print("\n");
  13.         do
  14.         {
  15.             System.out.print("MENU\n----------\n1.Equality\n2.Concatenate\n3.Replace\n4.Toggle\n5.Exit\n");
  16.             System.out.println("Enter your choice");
  17.             ch=sc.nextInt();
  18.             switch(ch)
  19.             {
  20.                 case 1: if(s1.equals(s2))
  21.                         System.out.println(s1+"="+s2);
  22.                     else
  23.                         System.out.println(s1+"!="+s2);
  24.                     break;
  25.                 case 2: System.out.println("The concatenated string is: "+s1.concat(s2));
  26.                     break;
  27.                 case 3: System.out.println("Enter string 1 or 2");
  28.                     int u=sc.nextInt();
  29.                     if(u==1)
  30.                     {
  31.                         System.out.println("Enter character to be replaced");
  32.                         String oldc=sc.next();//oldc=sc.next().charAt(0);
  33.                         System.out.print("Enter replacement character");
  34.                         String newc=sc.next();//char newc=sc.next().charAt(0);
  35.                         String m=s1.replace(oldc,newc);
  36.                         System.out.println("Modified String: "+m);
  37.                         break;
  38.                     }
  39.                     else if(u==2)
  40.                     {
  41.                         System.out.println("Enter character to be replaced");
  42.                         String oldc=sc.next();//oldc=sc.next().charAt(0);
  43.                         System.out.print("Enter replacement character");
  44.                         String newc=sc.next();//char newc=sc.next().charAt(0);
  45.                         String m=s2.replace(oldc,newc);
  46.                         System.out.println("Modified String: "+m);
  47.                         break;
  48.                     }
  49.                     else
  50.                     {
  51.                         System.out.print("Invalid choice");
  52.                         break;
  53.                     }
  54.                     /*for(int i=0;i<s1.length();i++)
  55.                     {
  56.                         if(s1.charAt(i)==oldc)
  57.                             m+=newc;
  58.                         else
  59.                             m+=s1.charAt(i);
  60.                     }*/
  61.                 case 4: s1.toUpperCase();
  62.                     break;
  63.                 case 5: System.exit(0);
  64.                     System.out.print("\n");        
  65.                 default: System.out.println("Invalid choice");
  66.             }
  67.         }while(ch<=5);
  68.     }
  69. }
  70.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement