petyaminkova91

Untitled

Dec 7th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel.Design;
  3. using System.Linq;
  4.  
  5. namespace _01_Username
  6. {
  7. class Username
  8. {
  9. static void Main(string[] args)
  10. {
  11. string username = Console.ReadLine();
  12.  
  13. string input = Console.ReadLine();
  14.  
  15. while (input != "Sign up")
  16. {
  17. string[] commandArgs = input.Split();
  18.  
  19. string command = commandArgs[0];
  20.  
  21. switch (command)
  22. {
  23. case "Case":
  24. string caseType = commandArgs[1];
  25. username = ChangeCaseTypeAndPrint(caseType, username);
  26. break;
  27. case "Reverse":
  28. int startIndex = int.Parse(commandArgs[1]);
  29. int endIndex = int.Parse(commandArgs[2]);
  30. ReverseSubstringAndPrintIt(startIndex, endIndex, username);
  31. break;
  32. case "Cut":
  33. string substring = commandArgs[1];
  34. username = CutAndPrintSubstringIfExist(username, substring);
  35. break;
  36. case "Replace":
  37. string charAsString = commandArgs[1];
  38. username = username.Replace(charAsString, "*");
  39. Console.WriteLine(username);
  40. break;
  41. case "Check":
  42. charAsString = commandArgs[1];
  43. CheckIfPasswordIsValid(username, charAsString);
  44. break;
  45. }
  46.  
  47. input = Console.ReadLine();
  48. }
  49. }
  50.  
  51. private static void CheckIfPasswordIsValid(string username, string charAsString)
  52. {
  53. if (username.Contains(charAsString))
  54. {
  55. Console.WriteLine("Valid");
  56. }
  57. else
  58. {
  59. Console.WriteLine($"Your username must contain {charAsString}.");
  60. }
  61. }
  62.  
  63. private static string CutAndPrintSubstringIfExist(string username, string substring)
  64. {
  65. if (username.Contains(substring))
  66. {
  67. username = username.Replace(substring, "");
  68. Console.WriteLine(username);
  69. }
  70. else
  71. {
  72. Console.WriteLine($"The word {username} doesn't contain {substring}.");
  73. }
  74.  
  75. return username;
  76. }
  77.  
  78. private static void ReverseSubstringAndPrintIt(int startIndex, int endIndex, string username)
  79. {
  80. bool isIndexValid = CheckIfIndexIsValid(startIndex, endIndex, username);
  81.  
  82. if (isIndexValid)
  83. {
  84. int length = endIndex - startIndex + 1;
  85. string substring = username.Substring(startIndex, length);
  86. char[] substringAsAChar = substring.Reverse().ToArray();
  87. substring = new string(substringAsAChar);
  88. Console.WriteLine(substring);
  89. }
  90. }
  91.  
  92. private static bool CheckIfIndexIsValid(int startIndex, int endIndex, string username)
  93. {
  94. if (0 <= startIndex && startIndex <= endIndex && 0 <= endIndex && endIndex < username.Length)
  95. {
  96. return true;
  97. }
  98.  
  99. return false;
  100. }
  101.  
  102. private static string ChangeCaseTypeAndPrint(string caseType, string username)
  103. {
  104. if (caseType == "lower")
  105. {
  106. username = username.ToLower();
  107. }
  108. else if (caseType == "upper")
  109. {
  110. username = username.ToUpper();
  111. }
  112.  
  113. Console.WriteLine(username);
  114. return username;
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment