task2.cs 766 B

123456789101112131415161718192021222324252627282930313233343536
  1. //2
  2. static void Main(string[] args)
  3. {
  4. Console.WriteLine("Введите значение:");
  5. string originalString = Console.ReadLine();
  6. string packedString = PackString(originalString);
  7. Console.WriteLine("Packed String: " + packedString);
  8. }
  9. static string PackString(string originalString)
  10. {
  11. if (string.IsNullOrEmpty(originalString))
  12. {
  13. return "";
  14. }
  15. string packed = "";
  16. int count = 1;
  17. for (int i = 0; i < originalString.Length; i++)
  18. {
  19. if (i == originalString.Length - 1 || originalString[i] != originalString[i + 1])
  20. {
  21. packed += count.ToString() + originalString[i];
  22. count = 1;
  23. }
  24. else
  25. {
  26. count++;
  27. }
  28. }
  29. return packed;
  30. }