Quick question. New to C# and as practice made a simple guessing game, but trying to make the guess case insensitive. In the below example the secret word = "cow", but I want "Cow" or "COW" to also be accepted.
I tried force the guess variable to lower case by using guess.ToLower();
, but it is not working. Any suggestions or alternatives?
Thanks all. Much appreciated,
Rory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SecretWord
{
class Program
{
static void Main(string[] args)
{
string secretWord = ("cow");
string guess = "", hint0 = "It's an animal!", hint1 = "Some of us have black and white coats!", hint2 = "You can often find me on a farm!", hint3 = "Moo!";
guess.ToLower();
int guesscount = 1;
while (guess != secretWord && guesscount != 5)
{
if (guesscount == 1)
{
Console.WriteLine($"Guess the secret word! {hint0}");
Console.Write("Enter a guess: ");
guess = Console.ReadLine();
guess.ToLower();
if (guess == secretWord)
{
break;
}
else
{
Console.WriteLine($"3 more tries. Here is a hint: {hint1}.");
}
}
if (guesscount == 2)
{
Console.Write("Enter a guess: ");
guess = Console.ReadLine();
if (guess == secretWord)
{
break;
}
else
{
Console.WriteLine($"2 more tries. Here's another hint: {hint2}.");
}
}
if (guesscount == 3)
{
Console.Write("Enter a guess: ");
guess = Console.ReadLine();
if (guess == secretWord)
{
break;
}
else
{
Console.WriteLine($"One last try and one last hint: {hint3}.");
}
}
if (guesscount == 4)
{
Console.Write("Enter a guess: ");
guess = Console.ReadLine();
if (guess == secretWord)
{
break;
}
else
{
Console.Write($"Too bad! ");
}
}
guesscount = guesscount + 1;
};
if (guess == secretWord)
{
Console.WriteLine("You win!");
}
else
{
Console.Write("Better luck next time!");
}
Console.ReadKey();
}
}
}