if I am given an string which is 1111 (no less than length 4) or 255255255255(no more than length 12).

I want to split up these numbers in every possible combination and create all possible valid IP addresses from them. For example,

for a given string "1902426"

The result should be

"1.90.24.26" "1.90.242.6" "19.0.24.26" "19.0.242.6" "190.2.4.26" "190.2.42.6" "190.24.2.6"

Ofcourse you cannot have anything with a leading "0", like "19.02.42.26"

Recommended Answers

All 7 Replies

Do your own homework.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace daniweb
{
  public partial class frmIP : Form
  {
    public frmIP()
    {
      InitializeComponent();
    }

    private static IPAddress[] GetList(string IP)
    {
      if (string.IsNullOrEmpty(IP))
        throw new ArgumentNullException("IP");
      if ((IP.Length < 4) || (IP.Length > 12))
        throw new ArgumentOutOfRangeException("IP");

      List<IPAddress> result = new List<IPAddress>();

      for (int i1 = 1; i1 <= (IP.Length >= 6 ? 3 : (IP.Length-3)); i1++)
      {
        string a = IP.Substring(0, i1);
        if (a.Equals("0"))
          continue;
        for (int i2 = 1; i2 <= ((IP.Length - i1) >= 5 ? 3 : ((IP.Length - i1) - 2)); i2++)
        {
          string b = IP.Substring(i1, i2);
          for (int i3 = 1; i3 <= ((IP.Length - (i1+i2)) >= 4 ? 3 : ((IP.Length - (i1+i2)) - 1)); i3++)
          {
            string c = IP.Substring(i1 + i2, i3);
            int remaining = IP.Length - (i1 + i2 + i3);
            string d = IP.Substring(i1 + i2 + i3, remaining);
            if (d.Length <= 3)
            {
              IPAddress addr = GetIP(a, b, c, d);
              if (addr != null)
                result.Add(addr);
            }
          }
        }
      }
      return result.ToArray();
    }

    private static IPAddress GetIP(char a, char b, char c, char d)
    {
      return GetIP(Convert.ToString(a), Convert.ToString(b), Convert.ToString(c), Convert.ToString(d));
    }
    private static IPAddress GetIP(string a, string b, string c, string d)
    {
      if (ValidSegment(a) && ValidSegment(b) && ValidSegment(c) && ValidSegment(d))
        return IPAddress.Parse(a + "." + b + "." + c + "." + d);
      else
        return null;
    }

    private static bool ValidSegment(string s)
    {
      if (string.IsNullOrEmpty(s))
        throw new ArgumentNullException("s");
      if (s.StartsWith("0") && !s.Equals("0"))
        return false;
      int i1;
      if (!int.TryParse(s, out i1))
        return false;
      if ((i1 < 0) || (i1 > 255))
        return false;
      return true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string input = textBox1.Text;

      Console.WriteLine("output for " + input + ":");
      IPAddress[] addrs = GetList(input);
      addrs.ToList().ForEach(delegate(IPAddress i) { Console.WriteLine(i.ToString()); });
      Console.WriteLine("---");
    }
    

  }
}

Results in:

output for 1111:
1.1.1.1
---
output for 255255255255:
255.255.255.255
---
output for 2552552552:
255.255.25.52
255.255.255.2
---
output for 1902426:
1.90.24.26
1.90.242.6
19.0.24.26
19.0.242.6
190.2.4.26
190.2.42.6
190.24.2.6
---

[edit]
I added a check for a="0" and you could add more checks under each loop to see if the remaining string count will be too long. It will save a few cpu cycles, but it will work the same.
[/edit]

commented: you just couldn't help yourself, could you -1
commented: Nice help +1
commented: You and your post can't deserve orange. +6
commented: works real good. I used IPAddress.TryParse() method to validate the IP address. It's an one liner and works well. Otherwise everything works great. +0

That's awesome. I have been breaking my head with this.

Thanks again.

Yea, your post was pinged by one of the best.... Now mark as solved and give sknake some +rep for his work.

Do your own homework.

I'm about tired of you. Do us all a favor and get banned again. Thanks.

Seriously... -rep for answering a question just because you refuse to? Get over yourself.

I'm about tired of you. Do us all a favor and get banned again. Thanks.

Why don't you do yourself a favor and start charging money to do people's homework for them.

Why don't you do yourself a favor and start charging money to do people's homework for them.

Rashakil,

That is unfounded speculation. While I think it very well could be if I played by your rules that is a -rep offfense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.