mattnguyen

My Retake Final Exam 13/12/2021 Problem 1

Dec 13th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. namespace ConsoleApp33
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. StringBuilder username = new StringBuilder().Append(Console.ReadLine());
  11.  
  12. string input = Console.ReadLine();
  13.  
  14. while (input != "Registration")
  15. {
  16. string[] commands = input.Split();
  17.  
  18. string command = commands[0];
  19. string temp = string.Empty;
  20.  
  21. switch (command)
  22. {
  23. case "Letters":
  24. if (commands[1] == "Lower")
  25. {
  26. temp = username.ToString().ToLower();
  27. username.Clear();
  28. username.Append(temp);
  29. }
  30. else
  31. {
  32. temp = username.ToString().ToUpper();
  33. username.Clear();
  34. username.Append(temp);
  35. }
  36. break;
  37.  
  38. case "Reverse":
  39. int startIndex = int.Parse(commands[1]);
  40. int endIndex = int.Parse(commands[2]);
  41.  
  42. if (startIndex >= 0 && startIndex < username.Length &&
  43. endIndex >= 0 && endIndex < username.Length
  44. )
  45. {
  46. var reversed = username.ToString().Substring(startIndex, endIndex - startIndex + 1).Reverse();
  47.  
  48. Console.WriteLine(string.Join("", reversed));
  49. }
  50. break;
  51. case "Substring":
  52. string substring = commands[1];
  53. string name = username.ToString();
  54.  
  55. if (name.Contains(substring))
  56. {
  57. startIndex = name.IndexOf(substring);
  58. username.Remove(startIndex, substring.Length);
  59. }
  60. else
  61. {
  62. Console.WriteLine($"The username {username.ToString().Trim()} doesn't contain {substring}.");
  63.  
  64. input = Console.ReadLine();
  65. continue;
  66. }
  67.  
  68. break;
  69. case "Replace":
  70. char character = char.Parse(commands[1]);
  71. username.Replace(character, '-');
  72. break;
  73.  
  74. default:
  75. character = char.Parse(commands[1]);
  76. if (username.ToString().Contains(character))
  77. {
  78. Console.WriteLine("Valid username.");
  79. }
  80. else
  81. {
  82. Console.WriteLine($"{character} must be contained in your username.");
  83. }
  84. break;
  85. }
  86. if (command == "Reverse" || command == "IsValid")
  87. {
  88. input = Console.ReadLine();
  89. continue;
  90. }
  91.  
  92. Console.WriteLine(username.ToString().Trim());
  93. input = Console.ReadLine();
  94. }
  95. }
  96. }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment