Answer to Question #204579 in C# for john smanthen

Question #204579

HOW CAN I FORCE THE USER TO ENTER 26 DISTINCTIVE CAPITAL LETTERS IN C# pleaseee post solution


1
Expert's answer
2021-06-09T04:25:27-0400


using System;
using System.Collections.Generic;

namespace CapitalLetters
{
    class Program
    {
        static void ShowMessage(string message, ConsoleColor messageColor)
        {
            Console.ForegroundColor = messageColor;
            Console.WriteLine(message);
            Console.ResetColor();
            Console.WriteLine(new string('-', 50));
        }

        static void Main(string[] args)
        {
            var allowedLetters = new List<char>();
            for (char i = 'A'; i <= 'Z'; i++) //adding capital letters to list
                allowedLetters.Add(i);

            var acceptedLetters = new List<char>(); //here we will store accepted letters
            int lettersQuantity = 26;

            while (acceptedLetters.Count != 26)
            {
                Console.WriteLine($"You must enter {lettersQuantity} distinctive capital letters:");
                if (!char.TryParse(Console.ReadLine(), out char enteredChar)) //check, that the input is only ONE char
                {
                    ShowMessage("Error! You must enter just ONE capital letter!", ConsoleColor.Red);
                    continue;
                }

                if (!char.IsUpper(enteredChar) || !char.IsLetter(enteredChar)) //check, that input is a capital letter
                {
                    ShowMessage("Error! You must enter a capital letter!", ConsoleColor.Red);
                    continue;
                }

                if (!allowedLetters.Contains(enteredChar)) //check, that input is allowed
                {
                    ShowMessage($"Letter \"{enteredChar}\" already accepted.", ConsoleColor.Yellow);
                    continue;
                }

                ShowMessage($"Letter \"{enteredChar}\" accepted.", ConsoleColor.Green); //all checks done

                lettersQuantity--;
                allowedLetters.Remove(enteredChar);
                acceptedLetters.Add(enteredChar);
            }

            Console.WriteLine($"Thank you! All {acceptedLetters.Count} capital letters accepted.");
        }
    }
}

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