Advertisement
Guest User

1

a guest
Jul 21st, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class demoExam {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String command = scanner.nextLine();
  7. String result = "";
  8. while (!"End".equals(command)) {
  9. String[] elements = command.split("\\s+");
  10. switch (elements[0]) {
  11. case "Add":
  12. result += elements[1];
  13. break;
  14. case "Upgrade":
  15. String ch1 = elements[1];
  16. String newResult = "";
  17. for (int i = 0; i < result.length(); i++) {
  18. if (String.valueOf(result.charAt(i)).equals(ch1)) {
  19. newResult += (char) (result.charAt(i) + 1);
  20. } else {
  21. newResult += String.valueOf(result.charAt(i));
  22. }
  23. }
  24. result = newResult;
  25. break;
  26. case "Print":
  27. System.out.println(result);
  28. break;
  29. case "Index":
  30. String ch2 = elements[1];
  31. boolean isIndexFound = false;
  32. int index = result.indexOf(ch2);
  33. while (index != -1) {
  34. System.out.print(index + " ");
  35. index = result.indexOf(ch2, index + 1);
  36. isIndexFound = true;
  37. }
  38. if (isIndexFound) {
  39. System.out.println();
  40. }
  41. if (!isIndexFound) {
  42. System.out.println("None");
  43. }
  44. break;
  45. case "Remove":
  46. String ch3 = elements[1];
  47. result = result.replace(ch3,"");
  48. break;
  49. }
  50.  
  51. command = scanner.nextLine();
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement