Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _23.ReplaceConsecutiveIdenticalLetters
- {
- class ReplaceConsecutiveIdenticalLetters
- {
- /* Write a program that reads a string from the
- * console and replaces all series of consecutive
- * identical letters with a single one. Example:
- * "aaaaabbbbbcdddeeeedssaa" "abcdedsa".*/
- static List<char> chars = new List<char>();
- static List<int> counts = new List<int>();
- static void Main()
- {
- // Console.WriteLine("Please enter sequence of chars:");
- string inputString = "aaaaabbbbbcdddeeeedssaat";// "aaatAaabbbb bcdddee@# eed125 ssaatt";// Console.ReadLine();
- string letters = "abcdefghijklmnopqrstuvwxyzABCDEFJHIGKLMNOPQRSTUVWXYZ";
- int count;
- Console.WriteLine("char-->count");
- for (int i = 0; i < inputString.Length; i++)
- {
- char currentChar = inputString[i];
- count = 1;
- if (letters.Contains(currentChar))
- {
- if (i == inputString.Length - 1)
- {
- if (currentChar != inputString[inputString.Length - 2])
- {
- AddCharAndCount(currentChar, 1);
- PrintCharToCount(currentChar, 1);
- }
- else
- {
- break;
- }
- }
- else
- {
- for (int j = i + 1; j < inputString.Length; j++)
- {
- char currentY = inputString[j];
- if (currentChar == inputString[j])
- {
- count++;
- }
- else
- {
- AddCharAndCount(currentChar, count);
- PrintCharToCount(currentChar, count);
- i = j - 1;
- count = 1;
- break;
- }
- if (j == inputString.Length - 1)
- {
- AddCharAndCount(currentChar, count);
- PrintCharToCount(currentChar, count);
- i = j - 1;
- }
- }
- }
- }
- }
- Console.WriteLine();
- Console.WriteLine("or");
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < chars.Count; i++)
- {
- builder.Append(chars[i]);
- Console.WriteLine("{0} --> {1}", chars[i], counts[i]);
- }
- Console.WriteLine(builder.ToString());
- }
- private static void AddCharAndCount(char currentChar, int count)
- {
- chars.Add(currentChar);
- counts.Add(count);
- }
- static void PrintCharToCount(char charOfString, int count)
- {
- Console.WriteLine("{0} --> {1}", charOfString, count);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement