Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I fix the output from always displaying 0 in the console?

I am writing a program that should split a string into groups of the same letter, then for each group it should count the number of uppercase and lowercase letters and multiply them together, then add the value for each group to a total for the entire string and output that total value.

So for example if the string was "ccCeEEEcBbBbb" in the first group of letters (ccC) there are two lowercase letters and one uppercase letter (2 x 1), in the second group of letters (eEEE) there is one lowercase letter and three uppercase letters (1 x 3), in the third group of letters (c) there is one lowercase letter and no uppercase letters (1 x 0) and in the last group of letters (BbBbb) there are three lowercase letters and two uppercase letters (3 x 2) – so the total would be (2 x 1) + (1 x 3) + (1 x 0) + (3 x 2) = 2 + 3 + 0 + 6= 11.

Here is the code I’ve written so far:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//string[] letters = { "a" };
//string[] letters =  { "aA" };
//string[] letters = { "Aa" };
//string[] letters = { "AAa" };
//string[] letters =  { "aaA" };
//string[] letters = { "aAa" };
//string[] letters =  { "AaA" };
//string[] letters = { "aAabB" };
//string[] letters =  { "aAabBB" };
//string[] letters = { "AAaBbBaaA" };
string[] letters =  { "AaAbAaA" };


//string[] letters = { "ccCeEEEcBbBbb" };

foreach (string letter in letters)
{
    int total = 0;
    int upper = 0;
    int lower = 0;
    StringBuilder sb = new StringBuilder(letter.Length);
    char lastChar = '\0';
    foreach (char c in letter)
    {
        if (c != lastChar)
        {
            sb.Append(c);
            lastChar = c;
        }
    }
    string data = sb.ToString().ToLower();
    for (int i = 0; i < data.Length; i++)
    {
        if (char.IsUpper(data[i]))
        {
            upper++;
        }
        else
        {
            lower++;
        }
    }
    total = upper * lower;
    Console.WriteLine(total);
}

The problem I am facing so far

The output in the console always shows 0 regardless of the test data.

Argument                             : Expected           : Actual
----------------------------------------------------------------------
a                                    : 0                  : 0 
aA                                   : 1                  : 0
Aa                                   : 1                  : 0
AAa                                  : 2                  : 0
aaA                                  : 2                  : 0

I went to the watch window in the debugger and all the variables comes up with values but I still don’t know where the error might be?

I have a hunch that the use of char might be the problem.

>Solution :

You can implement a simple Finite State Machine:

private static int SolveIt(string value) {
  int result = 0;

  char prior = '\0';

  int lower = 0;
  int upper = 0;

  foreach (char letter in value) {
    if (prior != char.ToLower(letter)) {
      result += lower * upper;

      lower = 0;
      upper = 0;

      prior = char.ToLower(letter);
    }

    if (char.IsLower(letter))
      lower += 1;
    else
      upper += 1;
  }

  return result + lower * upper;
}

Demo:

int result = SolveIt("ccCeEEEcBbBbb");

Console.WriteLine(result);

Output:

11
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading