MediumStringsNot started

Locker code check

A gym prints a code on every locker key tag. A valid code is exactly six characters: two upper-case letters A-Z followed by four digits 0-9, for example KD4471. The front desk scans a batch of tags and needs to know which codes are genuine.

Write a method bool IsValidCode(string code) and use it to check every code in the batch. For each code print the code followed by valid or invalid.

Input

The first line is an integer n. Each of the next n lines is one code: between 1 and 10 characters with no spaces.

Output

n lines, one per code in input order: CODE valid or CODE invalid.

Example 1

Input

4
KD4471
kd4471
KD447
AB12C4

Output

KD4471 valid
kd4471 invalid
KD447 invalid
AB12C4 invalid

The second code has lower-case letters, the third is only five characters, and the fourth has a letter where a digit must be.

Constraints

  • 1 <= n <= 100
  • Each code has 1 to 10 characters and contains no spaces

Hints

Hint 1 of 3

Check the length first; if it is not 6, nothing else matters and you can return early.

Hint 2 of 3

Index into the string with code[i] to get a char. Characters compare like numbers, so c >= 'A' && c <= 'Z' tests for a capital letter.

Hint 3 of 3

Positions 0 and 1 must be letters and positions 2 to 5 must be digits; return false the moment one position fails, and true only after all six pass.

Solution

Show a reference solution and explanation
 C# · reference solution
int count = int.Parse(Console.ReadLine()!);
for (int i = 0; i < count; i++)
{
    string code = Console.ReadLine()!;
    Console.WriteLine(code + (IsValidCode(code) ? " valid" : " invalid"));
}

static bool IsValidCode(string code)
{
    if (code.Length != 6) return false;
    for (int i = 0; i < 2; i++)
    {
        if (code[i] < 'A' || code[i] > 'Z') return false;
    }
    for (int i = 2; i < 6; i++)
    {
        if (code[i] < '0' || code[i] > '9') return false;
    }
    return true;
}

Why it works

A validation method reads best as a series of early exits: each if rejects one way the code can be wrong, and reaching the end means every rule held. Checking Length first also makes the later indexing safe, because code[5] on a five-character string would throw an IndexOutOfRangeException. Comparing char values against 'A' and 'Z' works because a char is a 16-bit number and the capital letters occupy a contiguous range; char.IsAsciiLetterUpper and char.IsAsciiDigit (available since .NET 7) express the same tests by name. Note the ternary condition ? a : b in the loop, which picks the suffix without a second if.

Your program
int count = int.Parse(Console.ReadLine()!);
for (int i = 0; i < count; i++)
{
    string code = Console.ReadLine()!;
    Console.WriteLine(code + (IsValidCode(code) ? " valid" : " invalid"));
}

static bool IsValidCode(string code)
{
    // return true only for two capital letters followed by four digits
    return false;
}
Run is not available for C# in the browser yet. Write your program here, then download it and run it locally with .NET SDK 8.0 against the examples above. The reference solution below was verified the same way.

Tests: 6 cases including the examples. Passing every test marks the exercise solved in this browser.

How this page was checked. Every program on it was run with .NET SDK 8.0 at build time by the publishing checks, and the output shown is what it printed. Running C# inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with .NET SDK 8.0 locally.