Advertisement
IrinaIgnatova

FinalExam - String Manipulator

Aug 3rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         Scanner scanner = new Scanner(System.in);
  14.  
  15.         String text = scanner.nextLine();////Thi5 I5 MY 5trING!//
  16.         String line = scanner.nextLine();//Translate 5 s
  17.  
  18.         while (!line.equals("End")) {
  19.  
  20.             String[] tokens = line.split(" +");
  21.             String command = tokens[0];
  22.  
  23.             if (command.equals("Translate")) {
  24.                 char charToReplace = tokens[1].charAt(0);
  25.                 char replacer = tokens[2].charAt(0);
  26.                 for (int i = 0; i < line.length(); i++) {
  27.                     char currentChar = line.charAt(i);
  28.                     if (currentChar == charToReplace) {
  29.                         text = text.replace(charToReplace, replacer);
  30.                     }
  31.  
  32.                 }
  33.                 System.out.println(text);
  34.             } else if (command.equals("Includes")) {
  35.                 String toCheck = tokens[1];
  36.                 if (text.contains(toCheck)) {
  37.                     System.out.println("True");
  38.                 } else {
  39.                     System.out.println("False");
  40.                 }
  41.             } else if (command.equals("Start")) {
  42.                 String[] textAsArray = text.split(" +");
  43.                 String currentStart = textAsArray[0];
  44.                 String startToChck = tokens[1];
  45.                 if (currentStart.equals(startToChck)) {
  46.                     System.out.println("True");
  47.                 } else {
  48.                     System.out.println("False");
  49.                 }
  50.             } else if (command.equals("Lowercase")) {
  51.                 text = text.toLowerCase();
  52.                 System.out.println(text);
  53.  
  54.             } else if (command.equals("FindIndex")) {
  55.                 char index = tokens[1].charAt(0);
  56.                 int lastIndexOf = (text.lastIndexOf(index));
  57.                 System.out.println(lastIndexOf);
  58.  
  59.             } else if (command.equals("Remove")) {
  60.                 int startIndex = Integer.parseInt(tokens[1]);
  61.                 int endIndex = Integer.parseInt(tokens[2]);
  62.                 if(startIndex>=0&&endIndex<text.length()){// if (index >= 0 && index < numbers.size())
  63.                     String toRemove = text.substring(startIndex, endIndex);
  64.                     text = text.replace(toRemove, "");
  65.                     System.out.println(text);
  66.                 }
  67.  
  68.  
  69.  
  70.             }
  71.  
  72.  
  73.             line = scanner.nextLine();
  74.         }
  75.  
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement