using System; namespace SequenceCount { class Program { // to count repeated characters in an array private void CountRepeatedCharacters() { string word = "aaabbcccd"; char[] characters = word.ToCharArray(); char startingCharacter = characters[0]; byte count = 1; string output = string.Empty; for (int i = 1; i < characters.Length; i++) { if (characters[i] == startingCharacter) { count++; } else { output = output + startingCharacter + count.ToString(); // set the current character to startingCharacter and set count to 1 startingCharacter = characters[i]; count = 1; } } output = output + startingCharacter + count.ToString(); Console.WriteLine("Output will be : " + output); Console.ReadLine(); } static void Main(string[] args) { Program objProgram = new Program(); objProgram.CountRepeatedCharacters(); } } }
C#, ASP.NET, ASP.NET MVC, OOPs, EWS Managed API, Web Application, Ms Sql Server, Javascript, JQUERY, CSS, LINQ, Entity Framework
Showing posts with label repeated characters count. Show all posts
Showing posts with label repeated characters count. Show all posts
Saturday, 8 August 2015
How to count repeated characters in an array
Subscribe to:
Posts (Atom)