Self-teaching myself c# I found a "coding challenge" for practice, but I am having a difficult time understanding how to do one of the requirements.
It absolutely has to be done in C# Console Application -just throwing that out there because I've already done my research and most of the answers I found were "use this instead."
The challenge is pretty easy to achieve, it's a Mablibs text version activity where you ask the user to input a noun, verb, etc.
What I've done so far has been creating two string arrays, one containing the different types of words it's going to ask the user:
string[] prompt = {"noun","verb","adverb"} //this contains 12 strings
and another array that contains the users input since I'm going to use a for loop to obtain their inputs, something similar to this:
For (int i = 0; i < userAnswer.Length; i++)
{
Console.Write("Please enter a/an " + prompt[i] + ": ");
userAnswer[i] = Console.ReadLine();
}
And of course I typed out the whole activity out to then display the users inputs.
BUT, I have to emphasize the changes and it says either to:
Underline the change - which I kept seeing that this isn't possible in Console App.
All Capitals - which would be the easy route, but I want to learn something different.
Bold the changes - I ran into StringBuilder and < b > < /b > mostly for this and tried it on my own, but wasn't able to get it to work.
A Different Color - I know I can use Console.ForegroundColor = ConsoleColor.Magenta;, but I only would want to change the color of what the user input. I saw many ways to "do it," but each time I tried it would change everything.
If anyone could provide some help, I would really appreciate that.
Thanks.
EDIT:
An example of what I'm trying to achieve
string[] answerHolder = {"","",""}; //MY originaly code has 13, but I am doing 3 to write it out faster
string[] prompt = {"noun", "verb", "adjective"};
Console.Readline("Help me finish the story:");
Console.Readline("A <noun> likes to eat a lot. It likes to <verb> in the <adjective> looking water. ");
//then it will ask the user to enter a noun, verb, and adjective
for(int i = 0; i < answerHolder.Length; i++)
{
Console.Write("Please enter a/an " + prompt[i] + ": ");
answerHolder[i] = Console.ReadLine();
}
Then let's say the user entered: bird, swim, cloudly
//Then I want to display it back but change the color of each
//element that was stored inside answerHolder to emphasize what they entered
Console.Writeline("A {0} likes to eat a lot. It likes to {1} in the {2} looking water.", answerHolder[0], answerHolder[1], answerHolder[2]);
//Code to change color or bold
Final Output: A bird likes to eat a lot. It likes to swim in the cloudly looking water.
I hope that helps you understand.
As Hans suggested, you will have to change Foreground color property around 6 times (as per your final output). The best you can do is put that logic in a loop.
Here's one way:
static void Main(string[] args)
{
string[] answerHolder = { "", "", "" }; //MY originaly code has 13, but I am doing 3 to write it out faster
string[] prompt = { "noun", "verb", "adjective" };
Console.WriteLine("Help me finish the story:");
Console.WriteLine("A <noun> likes to eat a lot. It likes to <verb> in the <adjective> looking water. ");
//then it will ask the user to enter a noun, verb, and adjective
for (int i = 0; i < answerHolder.Length; i++)
{
Console.Write("Please enter a/an " + prompt[i] + ": ");
answerHolder[i] = Console.ReadLine();
}
//Console.WriteLine("A {0} likes to eat a lot. It likes to {1} in the {2} looking water.", answerHolder[0], answerHolder[1], answerHolder[2]);
WriteFormattedLine("A {0} likes to eat a lot. It likes to {1} in the {2} looking water.", answerHolder);
Console.ReadLine();
}
private static void WriteFormattedLine(string format, params string[] answers)
{
int formatLength = format.Length;
int currIndex = 0;
bool readingNumber = false;
string numberRead = string.Empty;
while (currIndex < formatLength)
{
var ch = format[currIndex];
switch (ch)
{
case '{':
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Magenta;
readingNumber = true;
numberRead = string.Empty;
break;
case '}':
var number = int.Parse(numberRead);
var answer = answers[number];
Console.Write(answer);
Console.ResetColor();
readingNumber = false;
break;
default:
if (readingNumber)
numberRead += ch;
else
Console.Write(ch);
break;
}
currIndex++;
}
}
Note that this is very basic code. It blows up if the format is not as it expects. And you will have to put additional code if you want to print braces in your final output.