mattnguyen

My Problem 1 (Final Exam 14/08/2021)

Aug 20th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Problem1
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string text = Console.ReadLine();
  11.  
  12. string line = Console.ReadLine();
  13.  
  14. while (line != "Finish")
  15. {
  16. string[] commandData = line
  17. .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  18.  
  19. string command = commandData[0];
  20.  
  21. if (command == "Replace")
  22. {
  23. string substring = commandData[1];
  24. string replace = commandData[2];
  25.  
  26. if (text.Contains(substring))
  27. {
  28. text = text.Replace(substring, replace);
  29. }
  30. }
  31. else if (command == "Cut")
  32. {
  33. int startIndex = int.Parse(commandData[1]);
  34. int endIndex = int.Parse(commandData[2]);
  35.  
  36. if (IsValidIndex(text, startIndex) && IsValidIndex(text,endIndex))
  37. {
  38. StringBuilder sb = new StringBuilder();
  39.  
  40. for (int i = 0; i < text.Length; i++)
  41. {
  42. if (i >= startIndex && i <= endIndex)
  43. {
  44. continue;
  45. }
  46. else
  47. {
  48. sb.Append(text[i]);
  49. }
  50. }
  51.  
  52. text = sb.ToString();
  53. }
  54. else
  55. {
  56. Console.WriteLine($"Invalid indices!");
  57.  
  58. line = Console.ReadLine();
  59. continue;
  60. }
  61. }
  62. else if (command == "Make")
  63. {
  64. string filter = commandData[1];
  65.  
  66. if (filter == "Upper")
  67. {
  68. text = text.ToUpper();
  69. }
  70. else if (filter == "Lower")
  71. {
  72. text = text.ToLower();
  73. }
  74. }
  75. else if (command == "Check")
  76. {
  77. string substring = commandData[1];
  78.  
  79. if (text.Contains(substring))
  80. {
  81. Console.WriteLine($"Message contains {substring}");
  82. }
  83. else
  84. {
  85. Console.WriteLine($"Message doesn't contain {substring}");
  86. }
  87.  
  88. line = Console.ReadLine();
  89.  
  90. continue;
  91. }
  92. else if (command == "Sum")
  93. {
  94. int startIndex = int.Parse(commandData[1]);
  95. int endIndex = int.Parse(commandData[2]);
  96.  
  97. if (IsValidIndex(text,startIndex) && IsValidIndex(text,endIndex))
  98. {
  99. string substring = text.Substring(startIndex, endIndex - startIndex + 1);
  100.  
  101. int sum = 0;
  102.  
  103. foreach (var character in substring)
  104. {
  105. sum += character;
  106. }
  107.  
  108. Console.WriteLine(sum);
  109. }
  110. else
  111. {
  112. Console.WriteLine($"Invalid indices!");
  113. }
  114.  
  115. line = Console.ReadLine();
  116.  
  117. continue;
  118. }
  119.  
  120. Console.WriteLine(text);
  121.  
  122. line = Console.ReadLine();
  123. }
  124. }
  125.  
  126. private static bool IsValidIndex(string text, int startIndex)
  127. {
  128. return startIndex >= 0 && startIndex < text.Length;
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment