0

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();


        }
    }
}
Ruairí
  • 3
  • 1

3 Answers3

1

Strings in C# are immutable, which means that they cannot be changed. Whenever you want to change a string, you are creating a NEW string.

Notice that the ToLower() method returns a string. This means that it is going to take your current string in, and return you a new one that is all lowered. By default though, your original variable has not been modified.

To more clearly show this try doing this:

var guessToLower = guess.ToLower()

You will see that guessToLower has the guess all in lower case, but the guess variable has not been modified.

So to keep your code mostly the same, what you're going to want to do is assign the new string from ToLower back into your guess variable. It will look like this:

guess = guess.ToLower();

The other option is to not do ToLower() and instead use the .Equals method and tell it to ignore case (as Alexandru has show).

Lolop
  • 514
  • 2
  • 9
  • Thanks. This is a really clear explanation to help me understand what is actually going on. Thanks very much for taking the effort to explain in such detail! Will try it out now:) – Ruairí Mar 28 '21 at 13:10
  • No problem! Don't forget to mark this as answered so that it doesn't remain open. – Lolop Mar 28 '21 at 23:54
0

EAsY:

if (guess.Equals(secretWord, StringComparison.OrdinalIgnoreCase))
Alexandru Clonțea
  • 1,746
  • 13
  • 23
0

You may use the condition as the following:

 if(String.Equals(guess, secretWord, StringComparison.OrdinalIgnoreCase))
  {

  }

 Instead of it:

 if(guess == secretWord) 
  { 
         
  }
Salahuddin Ahmed
  • 4,854
  • 4
  • 14
  • 35