Answer to Question #302682 in C# for Alam

Question #302682

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.


1
Expert's answer
2022-02-25T10:29:18-0500
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);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS