Задание от codewars:
Write an algorithm that will identify valid IPv4 addresses in dot-decimal format. IPs should be considered valid if they consist of four octets, with values between 0 and 255, inclusive.
Input to the function is guaranteed to be a single string.
Examples Valid inputs:
1.2.3.4 123.45.67.89
Invalid inputs:
1.2.3 1.2.3.4.5 123.456.78.90 123.045.067.089 Note that leading zeros (e.g. 01.02.03.04) are considered invalid.
Мое решение:
class Kata
{
public static bool is_valid_IP(string ipAddres)
{
int ipnumber = 0;
bool numberChecker(List<char> number)
{
string checkNum = null;
foreach (char symb in number)
{
checkNum += symb.ToString();
}
try
{
byte result = Convert.ToByte(checkNum);
if (result.ToString() == (checkNum).ToString())
{
ipnumber++;
return true;
}
else
return false;
}
catch
{
return false;
}
}
bool getOut = false;
ipAddres = ipAddres + '.';
char[] ipArray = ipAddres.ToCharArray();
List<char> check = new List<char>();
foreach (char symbol in ipArray)
{
if (check.Contains('.'))
break;
if (symbol == '.')
{
getOut = numberChecker(check);
check.Clear();
}
else
check.Add(symbol);
}
if (getOut == true && ipnumber == 4)
{
return true;
}
else
return false;
}
}
code.cs(9,31): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='
code.cs(38,22): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
code.cs(38,33): error CS1519: Unexpected symbol `+' in class, struct, or interface member declaration
code.cs(43,19): error CS1519: Unexpected symbol `foreach' in class, struct, or interface member declaration
code.cs(43,35): error CS1519: Unexpected symbol `in' in class, struct, or interface member declaration
code.cs(43,44): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
code.cs(44,13): error CS9010: Primary constructor body is not allowed
code.cs(58,14): error CS1519: Unexpected symbol `if' in class, struct, or interface member declaration
code.cs(58,25): error CS1519: Unexpected symbol `==' in class, struct, or interface member declaration
code.cs(58,45): error CS1519: Unexpected symbol `==' in class, struct, or interface member declaration
code.cs(59,13): error CS9010: Primary constructor body is not allowed
code.cs(59,13): error CS8041: Primary constructor already has a body
code.cs(62,16): error CS1519: Unexpected symbol `else' in class, struct, or interface member declaration
code.cs(68,0): error CS1525: Unexpected symbol `}'
🤔 А знаете ли вы, что...
C# позволяет создавать собственные библиотеки классов и использовать их в других проектах.