Lesson 5
Handling and Manipulating Strings in C#
Introduction

Hello there! Today, we’re going to delve into handling and manipulating strings in C#, a skill that is invaluable in many areas of programming. Specifically, we'll learn how to identify consecutive groups of equal characters in a string. Are you curious to know more and enhance your skills? Then let's get started!

Task Statement

In this lesson, our goal is to write a C# function that accepts a string as input and identifies all consecutive groups of identical characters within it. A group can be defined as a segment of the text where the same character is repeated consecutively.

Your function should return a list of tuples. Each tuple will consist of the repeating character and the length of its repetition. For instance, if the input string is "aaabbcccaae", your function should output: [('a', 3), ('b', 2), ('c', 3), ('a', 2), ('e', 1)].

It's important to note that while processing the input string, we are interested only in alphanumeric characters (i.e., alphabets and digits), whether they are upper or lower case. Any other characters present won’t factor into the formation of these groups.

Are you ready to unravel how to accomplish this task? Let's dive in!

Solution Building, Step 1: Initialization

As is always the case when attempting to solve a problem, it's a good idea to take preliminary steps to establish our scope. First, we will initialize an empty list groups to store our results. We will also declare two variables, currentGroupChar and currentGroupLength, which will help us keep track of the current group's character and the length of its consecutive sequence.

Additionally, we will set up a foreach loop to iterate over each character in the string and an if condition within the loop to process only alphanumeric characters. These sections will be implemented in detail in the upcoming steps.

C#
1using System; 2using System.Collections.Generic; 3 4public class Solution 5{ 6 public static List<(char, int)> GetConsecutiveGroups(string s) 7 { 8 List<(char, int)> groups = new List<(char, int)>(); 9 char? currentGroupChar = null; 10 int currentGroupLength = 0; 11 // Identify the Groups in foreach loop 12 foreach(char ch in s) 13 { 14 // Additional logic will be added in the next steps 15 } 16 // Last check 17 if (currentGroupChar != null) 18 { 19 // Additional logic to be implemented 20 } 21 return groups; 22 } 23}
Step 2: Identify the Groups

During our loop, if a character is the same as currentGroupChar, it means that the group is continued, and we merely increment currentGroupLength. However, if the character is different from currentGroupChar, it signifies the start of a new group.

At the start of a new group, we append the tuple (currentGroupChar, currentGroupLength) to groups, and then update currentGroupChar and currentGroupLength with the new character and 1, respectively.

C#
1using System; 2using System.Collections.Generic; 3 4public class Solution 5{ 6 public static List<(char, int)> GetConsecutiveGroups(string s) 7 { 8 List<(char, int)> groups = new List<(char, int)>(); 9 char? currentGroupChar = null; 10 int currentGroupLength = 0; 11 12 foreach (char ch in s) 13 { 14 if (char.IsLetterOrDigit(ch)) 15 { 16 if (ch == currentGroupChar) 17 { 18 currentGroupLength++; 19 } 20 else 21 { 22 if (currentGroupChar != null) 23 { 24 groups.Add((currentGroupChar.Value, currentGroupLength)); 25 } 26 27 currentGroupChar = ch; 28 currentGroupLength = 1; 29 } 30 } 31 } 32 } 33}
Step 3: Wrap Up

After ending the loop, we need to be aware of the potential for a leftover group that has not yet been added to groups. This can happen because we only add a group to groups when we identify a new group. To ensure we don't miss any groups, we make a final check on currentGroup and, if necessary, add it to groups.

C#
1using System; 2using System.Collections.Generic; 3 4public class Solution 5{ 6 public static List<(char, int)> GetConsecutiveGroups(string s) 7 { 8 List<(char, int)> groups = new List<(char, int)>(); 9 char? currentGroupChar = null; 10 int currentGroupLength = 0; 11 12 foreach (char ch in s) 13 { 14 if (char.IsLetterOrDigit(ch)) 15 { 16 if (ch == currentGroupChar) 17 { 18 currentGroupLength++; 19 } 20 else 21 { 22 if (currentGroupChar != null) 23 { 24 groups.Add((currentGroupChar.Value, currentGroupLength)); 25 } 26 27 currentGroupChar = ch; 28 currentGroupLength = 1; 29 } 30 } 31 } 32 33 if (currentGroupChar != null) 34 { 35 groups.Add((currentGroupChar.Value, currentGroupLength)); 36 } 37 38 return groups; 39 } 40} 41 42public class Program 43{ 44 public static void Main(string[] args) 45 { 46 string input = "aaabbcccaae"; 47 List<(char, int)> result = Solution.GetConsecutiveGroups(input); 48 49 foreach (var group in result) 50 { 51 Console.WriteLine($"Character: {group.Item1}, Count: {group.Item2}"); 52 } 53 } 54}

When run with the input "aaabbcccaae", the expected output will be:

1Character: a, Count: 3 2Character: b, Count: 2 3Character: c, Count: 3 4Character: a, Count: 2 5Character: e, Count: 1
Lesson Summary

Congratulations! You have now learned how to identify consecutive groups of characters in a string using C#. This ability is very helpful, particularly when it comes to analyzing text-related problems, or even when preprocessing data for machine learning. Your next task is to work on similar problems to reinforce what you've learned and gain practice. Remember, mastery comes through persistence and continuous effort. Now get cracking on those strings!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.