Home > database >  How to skip over characters already looped through in a string
How to skip over characters already looped through in a string

Time:06-15

I have a function findChar() that loops through a string to find occurrences of characters in a string Ex: "Cincinnati" (2 Cs, 2 i's, etc) but once it finds another 'C' or 'i' it will return the values again

public static int findChar(string name, char c)
    {
        int count = 0;
        
        for (int i = 0; i < name.Length; i  )
        {
            if (name[i] == c || name[i] == Char.ToUpper(c) || name[i] == Char.ToLower(c))
            {
                count  ;
            }
                                             
        }
        return count;
    }

static void Main(string[] args)
    {
        
        string name = "Cincinnati";
        
        char c = ' ' ;
        int count = 0;

        for (int i = 0; i < name.Length; i  )
        {
            c = name[i];
            count = findChar(name, c);

            

            Console.WriteLine(count);
        }           
    }

My Output looks like this: 2 3 3 2 3 3 3 1 1 3

And I need it to be like: 2 3 3 1 1

CodePudding user response:

Option 1: keep track of the letters you already processed, and ignore it if you already did


Option 2: use System.Linq's GroupBy to get the count

    public static void Main()
    {
        var name = "Cincinatti";

        var letters = name.ToLower().GroupBy(letter => letter);

        foreach (var group in letters) {
            Console.WriteLine(group.Count());
        }
    }

CodePudding user response:

There are many ways to solve a problem like this. First let's discuss a problem it looks like you've already run into, capitalization. Lower case and upper case versions of the same letter are classified as different characters. The easiest way to combat this is to either convert the string to lowercase or uppercase so each duplicate letter can also be classified as a duplicate character. You can do this either by using the String.ToLower() method or the String.ToUpper() method depending on which one you want to use.

The way to solve this that is the closest to what you have is to just create a list, add letters to it as you process them, then use the list to check what letters you've processed already. It would look something like this:

static void Main(string[] args)
{
  string name = "Cincinnati";

  char c = ' ' ;
  int count = 0;
  var countedLetters = new List<string>();

  for (int i = 0; i < name.Length; i  )
  {
    c = name[i];
    char cLower = char.ToLower(c);

    if(countedLetters.Contains(cLower))
    {
      continue;
    }
    countedLetters.Add(cLower);

    count = findChar(name, c);
    Console.WriteLine(count);
  }
}

Although, you can usually use System.Linq's Enumerabl extension meethods to do things like this pretty easily.

Not deviating too much from what you have, another solution using System.Linq would be to just get the distinct characters and loop through that instead of the entire string. When doing this, we need to convert the entire string to either upper or lower case in order for linq to return the expected result. This would like something like this:

static void Main(string[] args)
{  
  string name = "Cincinnati";

  string nameLower = name.ToLower();
  int count = 0;

  foreach(char c in nameLower.Distinct())
  {
    count = findChar(name, c);

    Console.WriteLine(count);
  }           
}

Then finally, you can simplify this a ton by leaning heavily into the linq route. GroupBy is very useful for this because it's entire purpose is to group duplicates together. There are many ways to implement this and two have already be provided, so I will just provide a third.

public static void Main()
{
  string name = "Cincinatti";

  int[] counts = name.ToLower()
                 .GroupBy(letter => letter)
                 .Select(group => group.Count())
                 .ToArray();

  Console.WriteLine(string.Join("\n", counts));
}

CodePudding user response:

you can do group by list, sort optional (i left it commented out) and then select count

var word="Cincinnati";
        

    
        var groups = word.ToLower().GroupBy(n => {return n;})
                         .Select(n => new
                          {
                               CharachterName = n.Key,
                               CharachterCount = n.Count()
                          });
                        // .OrderBy(n => n.CharachterName);
        Console.WriteLine(JsonConvert.SerializeObject(groups.Select(i=>i.CharachterCount)));
  • Related