JulianJulianov

11.TextProcessingExercise-Replace Repeating Chars

Apr 19th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. 11.Replace Repeating Chars
  2. Write a program that reads a string from the console and replaces any sequence of the same letters with a single corresponding letter.
  3. Examples
  4. Input                                  Output
  5. aaaaabbbbbcdddeeeedssaa                abcdedsa
  6. qqqwerqwecccwd                         qwerqwecwd
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10.  
  11. public class Program
  12. {
  13.     public static void Main()
  14.     {
  15.          var repeatingChars = Console.ReadLine().ToCharArray();//Може и .ToList();
  16.             var replacedChars = new List<char>();
  17.             for (int i = 0; i < repeatingChars.Length; i++)    //Тогава "Length" става "Count - 1"!
  18.             {
  19.                 if (!replacedChars.Contains(repeatingChars[i]))
  20.                 {
  21.                     replacedChars.Add(repeatingChars[i]);
  22.                 }
  23.                 else if (repeatingChars[i - 1] != repeatingChars[i])
  24.                 {
  25.                     replacedChars.Add(repeatingChars[i]);
  26.                 }
  27.                 //var currentChar = repeatingChars[i];
  28.                 //var nextChar = repeatingChars[i+1];
  29.                 //if (currentChar == nextChar)
  30.                 //{
  31.                 //    repeatingChars.Remove(repeatingChars[i]);    // Може и RemoveAt(i);
  32.                 //    i--;
  33.                 //}
  34.             }
  35.             Console.WriteLine(string.Join("",replacedChars));       //Тук ще е "repeatingChars"
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment