How to ignore case sensitivity in a response from the user in c#?

Viewed 187

This is my very rudimentary guessing game. The answer is "luigi", but if I type "Luigi", it doesn't register as a valid answer because my secretWord (luigi) is not in the same caps. how do I get around this in c#?

These are my variables:

string secretWord1 = "luigi";

string guess = "";

int guessLimit = 0;

bool outOfGuesses = false;

int guessesLeft = 3;

This my guessing game:

        Console.WriteLine("He is a video game character");
        while (guess != secretWord1 && !outOfGuesses)
        {
            if (guessesLeft > guessLimit)
            {
                Console.Write("Enter guess: ");
                guess = Console.ReadLine();
                guessesLeft--;
                if (guessesLeft == 2 && guess != secretWord1)
                {
                    Console.WriteLine("He is a character in a Nintendo game");
                }
                if (guessesLeft == 1 && guess != secretWord1)
                {
                    Console.WriteLine("He is a character from the Mario Bros. games");
                }
                Console.WriteLine(guessesLeft);
            }
            else
            {
                outOfGuesses = true;
            }
        }
        if (outOfGuesses)
        {
            Console.WriteLine("You Lose");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("You Win!");
            Console.ReadLine();
        }
2 Answers

Change the 7th line to this

guess = Console.ReadLine().toLower();

Please see

Use the .Equals String Comparison

!guess.Equals(secretWord1, StringComparison.InvariantCultureIgnoreCase)
Related