How can I replace more than one occurrence of an input letter using indexOf? I can currently find all index occurrences if they end in -1. I'm also getting an unhanded exception when converting to char.
static Random rnd = new Random();
static void Main(string[] args)
{
string wrong = "";
ArrayList list = new ArrayList();
list.Add("Dune");
list.Add("The Lord of the Rings");
list.Add("The Iliad");
list.Add("Hamlet");
int r = rnd.Next(list.Count);
Console.WriteLine((string)list[r]);
string s = (string)list[r];
string patten = "[a-zA-Z0-9]";
Regex rgx = new Regex(patten);
string sDash = rgx.Replace(s, "-");
do
{
string a = " ";
Console.Write("Guess a letter: ");
a = Console.ReadLine();
while (a.Length != 1)
{
Console.WriteLine("Please enter only one letter:");
a = Console.ReadLine();
}
char myChar = a[0];
int input = s.IndexOf(a);
while (input != -1)
{
Console.WriteLine(input);
input = s.IndexOf(myChar, input + 1);
}
if (input != -1)
{
StringBuilder builder = new StringBuilder(sDash);
builder[input] = myChar;
sDash = builder.ToString();
Console.WriteLine(sDash + String.Compare(sDash, s, true));
}
else {
StringBuilder builder = new StringBuilder(wrong);
builder.Append(a);
string wrongAnswers = builder.ToString();
Console.WriteLine("Wrong, try again. \n {0}", wrongAnswers);
}
} while (String.Compare(sDash, s) == -1);
}