0

I'm making a hangman game in unity using C#.

I am using this code to check for letters in the word:

string s = "Hello World";

foreach(char o in s)
{
    Debug.Log(o);
}

I need to go over all the letters and check if there is the letter that the player entered like in my example o.

Then I need to replace the unknown letters represented by stars * with the letter that I checked for.

I have to keep track of where the letters are to later replace them.

Is there some way to keep track of the position of the letters?

Jan Hrubec
  • 59
  • 6
  • 1
    strings are array of chars, so _s[0]_ contains the letter 'H' and _s[1]_ contains the letter 'e' and so on. Could you explain for what reason you need to know the letter position (index) in the string (array)? – Steve Aug 06 '21 at 15:19
  • @Steve A string isn't technically an array of characters, but it does allow characters to be accessed by index. – Johnathan Barclay Aug 06 '21 at 15:24
  • 1
    @JohnathanBarclay Strings *are* arrays of characters i.e. a continuous block of memory cells like a train with wagons (2 bytes now with unicode and in .NET). This has always been the case with our silicon and transistor computers using integrated circuit memories technology, regardless of the generation and type of processors, and it will always be the case. But who knows with quantum or DNA computers and for example crystal mem... String are technically stored in a char array in .NET on any old and modern real computer, for performances reason, just like in C. –  Aug 06 '21 at 15:33
  • @JohnathanBarclay Of course the String class is an advanced wrapper toward an underlying char array. Maybe you meant that String is not a char[] as-is. Indeed. Not false. [Is string actually an array of chars or does it just have an indexer?](https://stackoverflow.com/questions/3669199/) • [How are String and Char types stored in memory in .NET?](https://stackoverflow.com/questions/10782690/) • [C# How to store a string](https://stackoverflow.com/questions/22879594/c-sharp-how-to-store-a-string) • [How string is stored in C#](https://stackoverflow.com/questions/44773527/) –  Aug 06 '21 at 15:40

2 Answers2

1

Strings in C# are immutable. So you have to create a new string containing the new guessed letter. If you are new at programming: divide your code into functions that do specific tasks.

A possible solution would be the following:

using System;
                    
public class Program
{
    public static void Main()
    {
        string answer = "Hello";
        // The length of the string with stars has to be the same as the answer.
        string newWord = ReplaceLetter('e', "*****", answer);
        Console.WriteLine(newWord);                             // *e***
        newWord = ReplaceLetter('x', newWord, answer);
        Console.WriteLine(newWord);                             // *e***
        newWord = ReplaceLetter('H', newWord, answer);
        Console.WriteLine(newWord);                             // He***    
        newWord = ReplaceLetter('l', newWord, answer);
        Console.WriteLine(newWord);                             // Hell*                
    }
    
    public static string ReplaceLetter(char letter, string word, string answer)
    {
        // Avoid hardcoded literals multiple times in your logic, it's better to define a constant.
        const char unknownChar = '*';
        string result = "";
        for(int i = 0; i < word.Length; i++)
        {
            // Already solved?
            if (word[i] != unknownChar) { result = result + word[i]; }
            // Player guessed right.
            else if (answer[i] == letter) { result = result + answer[i]; }
            else result = result + unknownChar;
        }
        return result;
    }
}
Michael
  • 1,166
  • 5
  • 4
0

Use a for loop instead:

for (int i = 0; i < s.Length; i++)
{
    char o = s[i];
    Debug.Log(o);
}

String has an indexer that can be used to obtain the character at the given index.

By using a for loop you have access to the index i of each iteration.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • I am sorry I'm not all that good at programming, how could use that index to replace the unknown letters with the letter "o" – Jan Hrubec Aug 06 '21 at 16:06