Advertisement
apez1

Assignment 5: Pivot Strings

Oct 19th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Assignment5_WIP {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.        
  10.         System.out.println("Enter the first String: ");
  11.         String string1 = scanner.nextLine();
  12.         int lengthofall = string1.length();
  13.        
  14.         System.out.println("Enter the pivot String");
  15.         String string2 = scanner.nextLine();
  16.        
  17.         Boolean flag = string1.contains(string2);
  18.        
  19.         if(string2 == null || flag == false) {
  20.             System.out.println("Error: Pivot String not found.");
  21.            
  22.         }
  23.        
  24.         else {
  25.             int indexof = string1.indexOf(string2);                                 //start of pivot string
  26.             int lengthof = string2.length();                                        //total length of string
  27.             String test = string1.substring(indexof, (indexof + lengthof));         //substring of only pivot
  28.            
  29.             String front = string1.substring(0, indexof);
  30.             String back = string1.substring((indexof + lengthof) ,lengthofall );
  31.            
  32.             System.out.println(back + string2 + front);
  33.         }
  34.    
  35.        
  36.        
  37.        
  38.        
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement