0

The problem: user enters an answer (a,b,c,d). the program should match from database and get the exact full answer.

Example:

What is 9 + 1?
a. 9
b. 10
c. 5
d. 21

When user writes (b) the program should get the answer which is (10)

here is what i did:

private string Answer(string question, string answer)
        {
            string userans = null;
            try
            {
                if (question != null || answer != null)
                {

                    string[] inputSplit = question.ToString().Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < inputSplit.Length; i++)
                    {
                        if (inputSplit[i].Contains(answer + "."))
                        {
                            userans = inputSplit[i].Split('.')[1];
                            return userans;
                        }
                        else
                        {
                            userans = null;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return userans;
        }

The question is: How i can avoid using Loop and extract the same result?. or is there a better way to do it?

3
  • Why are you calling ToString on question? It's already a string. Commented Aug 20, 2020 at 16:53
  • @juharr it's a habit i picked from the school. Commented Aug 20, 2020 at 16:55
  • An Object Oriented approach would eliminate loops and make everything simpler: A class with a Question, N answers and an indicator which is the correct one. Correct database storage may offer even more learning opportunities. Commented Aug 20, 2020 at 17:00

1 Answer 1

0

You could use Regex to create groups for each multiple choice option and then extract the answer that way.

This solutions assumes you only ever have 4 options and uses the format you used in the question. You will also want to add some input validation but this is the general idea.

public string GetAnswer(string question, string userAnswer)
{
    var regexPattern = "a\\. (\\w+)\r\nb\\. (\\w+)\r\nc\\. (\\w+)\r\nd\\. (\\w+)";
    var match = Regex.Match(question, regexPattern);

    if (match.Success)
    {
        switch (userAnswer)
        {
            case "a":
                return match.Groups[1].Value;

            case "b":
                return match.Groups[2].Value;

            case "c":
                return match.Groups[3].Value;

            case "d":
                return match.Groups[4].Value;
        }
    }

    throw new Exception("Failed to parse the question.");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Group group = Regex.Match(question, "a",RegexOptions.IgnoreCase); you mean this way? if not please provide an example
This solution addresses the question you originally asked but I think a better solution would be to create a class with some properties like public string Question and public List<string> PossibleAnswers and then have the user enter the integer index for the multiple choice answer. Then you could extract the answer from the List using the index.
true, but this wont be used a lot because some users enter the question and other answer it so im using this to fill the database with these question instead of doing it manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.