Find Numbers Gaps

Numbers Gaps is a small utility that is going to give you the missing numbers in a series .

Usage : if you are working with a serial numbers in your application and you want to know if one of the numbers in the middle is deleted it can cause a pain to your mind if you try to do it manually .

but if you have a tool that make this for you , it will be very good.

The Code :

create as GUI like the bellow screen shoot



Then add the following code to the ( Find Gaps) Button

int[] myData = StringToInts(txtsource.Text);

int mMin = myData.Min();
int mMax = myData.Max();

StringBuilder myStr = new StringBuilder();
int mCount = 0;
for (int ii = mMin; ii <= mMax; ii++)
{
    if (myData.Contains(ii) == false)
    {
        myStr.Append(ii.ToString() + Environment.NewLine);
        mCount++;
    }
}

txtResult.Text = myStr.ToString();
lblCount.Text = "Total Numbers Found : " + mCount.ToString();

and add the following method to the same class

private int[] StringToInts(string myString)
{
    List ints = new List();
    string[] strings;

    if (myString.Contains(","))
  strings = myString.Split(',');
    else
  strings = myString.Split(Environment.NewLine.ToCharArray());

    foreach (string s in strings)
    {
  int i;
  if (int.TryParse(s.Trim(), out i))
  {
   ints.Add(i);
  }
    }
    return ints.ToArray();
 }

No comments:

Post a Comment