Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class StringManipulator {
  4.  
  5.  
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String input = scanner.nextLine();
  10.  
  11. String command = scanner.nextLine();
  12.  
  13. while (!"End".equals(command)) {
  14. String[] tokens = command.split("\\s+");
  15. String firstCommand = tokens[0];
  16.  
  17. if ("Translate".equals(firstCommand)) {
  18. char firstChar = tokens[1].charAt(0);
  19. char replaceChar = tokens[2].charAt(0);
  20.  
  21. input = input.replace(firstChar, replaceChar);
  22. System.out.println(input);
  23.  
  24. }else if ("Includes".equals(firstCommand)) {
  25. String s = tokens[1];
  26.  
  27. if (input.contains(s)) {
  28. System.out.println("True");
  29. }else {
  30. System.out.println("False");
  31. }
  32. }else if ("Start".equals(firstCommand)) {
  33. String s = tokens[1];
  34.  
  35. if (input.startsWith(s)) {
  36. System.out.println("True");
  37. }else {
  38. System.out.println("False");
  39. }
  40. } else if ("Lowercase".equals(firstCommand)) {
  41. input = input.toLowerCase();
  42. System.out.println(input);
  43. } else if ("FindIndex".equals(firstCommand)) {
  44. char ch = tokens[1].charAt(0);
  45.  
  46. int result = input.lastIndexOf(ch);
  47.  
  48. System.out.println(result);
  49.  
  50.  
  51. } else if ("Remove".equals(firstCommand)) {
  52. int startIndex = Integer.parseInt(tokens[1]);
  53. int count = Integer.parseInt(tokens[2]);
  54.  
  55. StringBuilder sb = new StringBuilder();
  56. for (int i = 0; i < input.length(); i++) {
  57. sb.append(input.charAt(i));
  58. }
  59. sb.replace(startIndex, count, "");
  60. input = sb.toString();
  61.  
  62. System.out.println(input);
  63.  
  64. }
  65.  
  66. command = scanner.nextLine();
  67. }
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement