Hey.
I'm making a Hangman and I have this code to retrieve a random word from a txt file:

private void ord()
        {
            Random random = new Random();
            StreamReader read = new StreamReader("sweord.txt");
            ArrayList lines = new ArrayList();
            string line;
            
 
            while ((line = read.ReadLine()) != null) 
            lines.Add(line);
            read.Close();
 
             int randomLine = random.Next(0, lines.Count);
             
              string word;
              word = lines[randomLine].ToString();
              game = word;
                            
              for (int i = 0; i < game.Length; i++)
              {
                  maskedword += "_ ";
              }
            
 
/*(strings)*/ mword = maskedword;          //saving maskedword for further use as I have to reset it
              label5.Text = maskedword;
              maskedword = "";

              
        }

Maskedword is as you can see put into a label which shows "_ _ _ _ _". And this is where I'm stuck.
To check if the user has made a correct guess or not I have:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {            
            guess = e.KeyChar.ToString().ToUpper();           
            for (int i = 0; i < game.Length; i++)
            {
               
                if (game[i] == e.KeyChar)  //game is the randomed word
                {

                 }        
                  ...

Now. Let's say the word (game) is "Water" and the user guessed "t". I have to put the "t" where it belongs so it should look like this:
"_ _ t _ _"
I've been trying to do this in a few different ways but none works. The problem isn't in extracting the guessed letter but to put it into a string (or something else) in its correct position.

if (game[i] == e.KeyChar) 
                {


                    mword.Substring(i, 1) = game[i];
                   label5.Text = mword;
                    ...
                 }

One example above. I'm trying to put the letter into mword and then write it into the label but I get the error messages: "The lefthand side of an assignment must be a variable, property or indexer" and "Cannot implicitly convert type char to string."

I've tried some stuff with replace and char arrays without success.
Any thoughts?

Strings in C# are immutable! Look up somewhere why that is so, looked strange to me at first also. So if you want to change a string, you have to create a new one, many of the string manipulation methods do that all the time.
You could use this:

string mword = "------";
           char[] MyCharArray = mword.ToCharArray();
            MyCharArray[2] = 't';
            mword = new string(MyCharArray);

Now mword is equal to "--t---"

Hmm thanks to your post I got a little further. This is what I got now.

if (game[i] == e.KeyChar) 
                {
                     
                    char[] mychararray = mword.ToCharArray();
                    char[] array2 = guess.ToCharArray();

                    if (i % 2 == 0)
                    {
                        mychararray[i] = array2[0];                       
                    }
                    else
                    {
                        i = i + 1;
                        mychararray[i] = array2[0];
                    }
                    loop2 = new string(mychararray);
                    label5.Text = loop2;

The problem is that the array actually looks like this: "_ _ _ _ _ " with spaces in between and that should make every place i want to put a letter an odd number. However this won't work either. It will write "a" and "t" on the same place and the only thing that works well is pretty much the first letter. :(

Edited 6 Years Ago by ereruh: n/a

Ok nevermind i changed the

maskedword += "_ ";

to

maskedword += "-";

which shows a spacing between the characters without having to add a space. Thus I won't need to deal with the space issue. Thanks for your help ddanbe!

This question has already been answered. Start a new discussion instead.