Advertisement
Jbt3377

withoutString Problem

Nov 22nd, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class withoutString {
  4.  
  5.     public static void main(String[] args) {
  6.         // Gets base and remove strings
  7.         Scanner input = new Scanner(System.in);
  8.         System.out.print("Enter a Base String: ");
  9.         String base = input.nextLine();
  10.         System.out.print("Enter a Remove String: ");
  11.         String remove = input.nextLine();
  12.         input.close();
  13.  
  14.         // Informs user of Original Base String and Remove Substring before process starts
  15.         System.out.println("\nBase String: " + base);
  16.         System.out.println("Remove String: " + remove + "\n");
  17.  
  18.         // Defines necessary variables
  19.         boolean notFinished = true;
  20.         String newBase = "";
  21.  
  22.         // While loop runs until all instances of the Remove Substring are deleted from the Base string
  23.         // NOTE: This includes the edge case where Base: "heerere" Remove: "ere" -> NewBase: "h"
  24.  
  25.         while(notFinished){
  26.             // Runs method to delete a first instance of the remove substring. A new base is returned
  27.             newBase = findAndRemove(base, remove);
  28.  
  29.             if(newBase.equals(base)){
  30.                 // If base has changed, we repeat the process encase a new instance
  31.                 // of the Remove Substring was produced in the New Base
  32.                 notFinished = false;
  33.             }else{
  34.                 // If not, we can be sure there is no remaining instance of the Remove Substring
  35.                 // and we can terminate the loop
  36.                 base = newBase;
  37.             }
  38.         }
  39.  
  40.         // Output Final Base with no instance of the Remove Substring
  41.         System.out.println("New Base: " + base);
  42.     }
  43.  
  44.     /*
  45.      Method returns a new Base of type String, after a linear search has removed any
  46.      instances of the remove substring.
  47.       */
  48.     public static String findAndRemove(String base, String remove){
  49.         // Define necessary variables
  50.         String potentialRemove = "";
  51.         String beforeRemove, afterRemove, newBase;
  52.         String baseLower = base.toLowerCase();
  53.         String removeLower = remove.toLowerCase();
  54.  
  55.         // Linear searches Base for first character of Remove. This will locate any potential Remove instances
  56.         for(int i=0; i<=baseLower.length()-remove.length(); i++){
  57.  
  58.             if(baseLower.charAt(i) == removeLower.charAt(0)){
  59.                 // Potential remove substring located
  60.                 potentialRemove = baseLower.substring(i, i+remove.length());
  61.  
  62.                 if(potentialRemove.equals(remove)){
  63.                     // If this is a correctly identified remove instance;
  64.                     //      - Create a Base Substring before the Remove Instance
  65.                     beforeRemove = base.substring(0, i);
  66.                     //      - Create a Base Substring after the Remove Instance
  67.                     afterRemove = base.substring(i+remove.length());
  68.                     //      - Create a new Base using the two substrings, removing the Remove Instance
  69.                     newBase = beforeRemove + afterRemove;
  70.                     //      - Returns the new Base String
  71.                     return newBase;
  72.                 }
  73.             }
  74.         }
  75.  
  76.         return base;
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement