Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 11.Replace Repeating Chars
- Write a program that reads a string from the console and replaces any sequence of the same letters with a single corresponding letter.
- Examples
- Input Output
- aaaaabbbbbcdddeeeedssaa abcdedsa
- qqqwerqwecccwd qwerqwecwd
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- var repeatingChars = Console.ReadLine().ToCharArray();//Може и .ToList();
- var replacedChars = new List<char>();
- for (int i = 0; i < repeatingChars.Length; i++) //Тогава "Length" става "Count - 1"!
- {
- if (!replacedChars.Contains(repeatingChars[i]))
- {
- replacedChars.Add(repeatingChars[i]);
- }
- else if (repeatingChars[i - 1] != repeatingChars[i])
- {
- replacedChars.Add(repeatingChars[i]);
- }
- //var currentChar = repeatingChars[i];
- //var nextChar = repeatingChars[i+1];
- //if (currentChar == nextChar)
- //{
- // repeatingChars.Remove(repeatingChars[i]); // Може и RemoveAt(i);
- // i--;
- //}
- }
- Console.WriteLine(string.Join("",replacedChars)); //Тук ще е "repeatingChars"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment