Advertisement
Guest User

Untitled

a guest
Aug 17th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 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.  Example: "aaaaabbbbbcdddeeeedssaa" -> "abcdedsa".*/
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. class ConsecutiveIdenticalLetters
  7. {
  8.     static void Main()
  9.     {
  10.         string text = "aaaaabbbbbcdddeeeedssaa";
  11.  
  12.         List<string> wordWithoutRepeatingLetters = new List<string>();
  13.         wordWithoutRepeatingLetters.Add(text[0].ToString());
  14.  
  15.         GettingTheSequenceOfNonIdenticalLetters(text, wordWithoutRepeatingLetters);
  16.  
  17.         PrintingTheSequence(wordWithoutRepeatingLetters);
  18.     }
  19.  
  20.     private static void PrintingTheSequence(List<string> wordWithoutRepeatingLetters)
  21.     {
  22.         foreach (var letter in wordWithoutRepeatingLetters)
  23.         {
  24.             Console.Write(letter);
  25.         }
  26.         Console.WriteLine();
  27.     }
  28.  
  29.     private static void GettingTheSequenceOfNonIdenticalLetters(string text, List<string> wordWithoutRepeatingLetters)
  30.     {
  31.         for (int i = 1; i < text.Length; i++)
  32.         {
  33.             if (text[i] != text[i - 1])//If the current letter in not equal to the one before I add it to the List
  34.             {
  35.                 wordWithoutRepeatingLetters.Add(text[i].ToString());
  36.             }
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement