Advertisement
dimipan80

Series of Letters

May 12th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. // Write a program that reads a string from the console and replaces all series of consecutive identical letters with a single one.
  2.  
  3. namespace _01.SeriesOfLetters
  4. {
  5.     using System;
  6.     using System.Text;
  7.     using System.Text.RegularExpressions;
  8.  
  9.     class SeriesOfLetters
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string text = Console.ReadLine();
  14.             string pattern = @"(?<letter>\w)\1+";
  15.             MatchCollection matches = Regex.Matches(text, pattern);
  16.             StringBuilder sb = new StringBuilder(text);
  17.             foreach (Match match in matches)
  18.             {
  19.                 sb.Replace(match.ToString(), match.Groups["letter"].Value.ToString());
  20.             }
  21.  
  22.             Console.WriteLine(sb);
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement