I'm trying to create a program in c# that asks the user a question and then tells him if he is correct or incorrect, the code I am using only allows for case to be the same. Is there a way I can compare two strings without it checking for case. Here is some of my code

      Console.Write("List the first sahabi who was promised paradise \n(hint he was Prophet Muhammad (peace be upon him)'s                            best friend: ");
      answer = Console.ReadLine();
      if (answer == "Abu Bakr")
      {
         Console.WriteLine("That is correct good job!\n\n");
         ++score;
      }

Recommended Answers

All 3 Replies

You can use the ToUpper() or ToLower() on the input and then compare it to the correct answer in that same case. For example...

 if (answer.ToLower() == "abu bakr")
commented: Thanks a bunch +2

I think what you want is something like this:

        Console.Write("List the first sahabi who was promised paradise \n(hint he was Prophet Muhammad (peace be upon him)'s best friend: ");
        answer = Console.ReadLine();
        if(answer.ToLower() == "abu bakr")
        {
            Console.WriteLine("That is correct good job!\n\n");
            ++score;
        }

This converts the answer to all lower case and compares it to the right answer present in lower case.

commented: Thank you so much! Big help +2

how would I convert the input to all lowercase

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.