fork download
  1. using System;
  2. using System.Text;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. Console.Write("Enter string: ");
  9. var text = Console.ReadLine();
  10. StringBuilder sb = new StringBuilder("", text.Length);
  11. int count = 0;
  12. foreach (var el in text) {
  13. if (Char.IsWhiteSpace(el))
  14. count++;
  15. else {
  16. if (count==1)
  17. sb.Append('%',1);
  18. if (count>1)
  19. sb.Append(' ',1);
  20. sb.Append(el,1);
  21. count = 0;
  22. }
  23. }
  24. Console.WriteLine($"New string: {sb.ToString()}");
  25. }
  26. }
Success #stdin #stdout 0.02s 15936KB
stdin
qqq www   rrr
stdout
Enter string: New string: qqq%www rrr