Create a method that determines whether a string is a valid hex
code. A hex code must begin with a pound key # and is exactly 6
characters in length. Each character must be a digit from [0-9] or
an alphabetic character from A-F . All alphabetic characters may
be uppercase or lowercase.
using System;
namespace HexCodeValid
{
class Program
{
public static string Valid_hex(string hexcode)
{
string str = "ABCDEFabcdef";
if (hexcode[0] != '#')
return "A hex code must begin with a pound key #";
else if (hexcode.Length != 6)
return "A hex code must 6 characters in length";
else
{
for(int i=1; i < 6; i++)
{
if ((Char.IsNumber(hexcode[i]) == false) && (str.Contains(hexcode[i].ToString()) == false))
return "Each character must be a digit from [0-9] or an alphabetic character from A-F";
}
}
return "Hex code is Valid";
}
public static void Main(string[] args)
{
Console.Write("Enter Hex code: ");
string hex = Console.ReadLine();
Console.WriteLine(Valid_hex(hex));
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment