Advertisement
petyaminkova91

Untitled

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