Advertisement
desislava_topuzakova

01. Password Reset

Nov 25th, 2022
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. package exam_preparation;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ThirdTask {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String password = scanner.nextLine(); //парола за модификация
  10. String command = scanner.nextLine(); //команда или "Done"
  11.  
  12. while (!command.equals("Done")) {
  13. //команда
  14. if (command.equals("TakeOdd")) {
  15. //1. command = "TakeOdd"
  16. //1. взимаме символите на нечетни индекси (1, 3, 5, 7, 9, ...)
  17. password = getCharsOnOddPositions(password); //новообразувана парола
  18. //2. отпечатвам новата парола
  19. System.out.println(password);
  20. } else if (command.contains("Cut")) {
  21. //2. command = "Cut 2 5".split("\\s+") -> ["Cut", "2", "5"]
  22. int index = Integer.parseInt(command.split("\\s+")[1]); // 2
  23. int length = Integer.parseInt(command.split("\\s+")[2]); // 5
  24. //password = "TestZombie12345"
  25. //1. взимам от паролата текстът, който започва от index и е с дължина length
  26. String substringToRemove = password.substring(index, index + length); //текстът, който трябва да премахна -> "stZom"
  27. //2. премахна първото срещане на този текст от паролата
  28. password = password.replaceFirst(substringToRemove, "");
  29. System.out.println(password);
  30. } else if (command.contains("Substitute")) {
  31. //3. command = "Substitute est var".split("\\s+") -> ["Substitute", "est", "var"]
  32. //password = "TestZombie12345"
  33. String substring = command.split("\\s+")[1]; //текстът, който трябва да заменя -> "est"
  34. String substitute = command.split("\\s+")[2]; //текстът заместител -> "var"
  35.  
  36. if (password.contains(substring)) {
  37. //заменя всички substring със substitute
  38. password = password.replaceAll(substring, substitute); //"TvarZombie12345"
  39. System.out.println(password);
  40. } else {
  41. //в паролата да нямам текст за замяна
  42. System.out.println("Nothing to replace!");
  43. }
  44.  
  45. }
  46. command = scanner.nextLine();
  47. }
  48.  
  49. System.out.println("Your password is: " + password);
  50. }
  51.  
  52. private static String getCharsOnOddPositions(String password) {
  53. //password = "Desi1234"
  54. //всички символи на нечетни индекси (1, 3, 5, 7, 9...) -> 'е', 'i', '2', '4' -> "ei24"
  55. StringBuilder newPassword = new StringBuilder(); //конструирам новата парола
  56.  
  57. //вариант 1
  58. for (int position = 0; position <= password.length() - 1; position++) {
  59. //индекси / позиции от 0 до последната
  60. if (position % 2 != 0) {
  61. //позицията е нечетна
  62. char currentSymbol = password.charAt(position);
  63. newPassword.append(currentSymbol);
  64. }
  65. }
  66. //вариант 2:
  67. /*for (int position = 1; position <= password.length() - 1; position += 2) {
  68. //позицията е нечетна
  69. char currentSymbol = password.charAt(position);
  70. newPassword.append(currentSymbol);
  71. }*/
  72. return newPassword.toString();
  73. }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement