0
  1. Hi guys, so I need to add a 'space' between each character in my displayed text box.

  2. I am giving the user a masked word like this He__o for him to guess and I want to convert this to H e _ _ o

  3. I am using the following code to randomly replace characters with '_'

        char[] partialWord = word.ToCharArray();
    
        int numberOfCharsToHide = word.Length / 2;          //divide word length by 2 to get chars to hide 
        Random randomNumberGenerator = new Random();        //generate rand number
        HashSet<int> maskedIndices = new HashSet<int>();    //This is to make sure that I select unique indices to hide. Hashset helps in achieving this
        for (int i = 0; i < numberOfCharsToHide; i++)       //counter until it reaches words to hide
        {
            int rIndex = randomNumberGenerator.Next(0, word.Length);       //init rindex
            while (!maskedIndices.Add(rIndex))
            {
                rIndex = randomNumberGenerator.Next(0, word.Length); //This is to make sure that I select unique indices to hide. Hashset helps in achieving this
            }
            partialWord[rIndex] = '_';                      //replace with _
        }
        return new string(partialWord);   
    
  4. I have tried : partialWord[rIndex] = '_ ';however this brings the error "Too many characters in literal"

  5. I have tried : partialWord[rIndex] = "_ "; however this returns the error " Cannot convert type string to char.

Any idea how I can proceed to achieve a space between each character?

Thanks

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
rrz0
  • 2,182
  • 5
  • 30
  • 65
  • Not a duplicate (as far as I know) but with a bit of googling you would have found: http://stackoverflow.com/questions/7189293/add-spaces-between-the-characters-of-a-string-in-java of which you can almost directly copy the loops. – VinKel Jan 10 '17 at 08:04

2 Answers2

2

Since the resulting string is longer than the original string, you can't use only one char array because its length is constant.

Here's a solution with StringBuilder:

var builder = new StringBuilder(word);
for (int i = 0 ; i < word.Length ; i++) {
    builder.Insert(i * 2, " ");
}
return builder.ToString().TrimStart(' '); // TrimStart is called here to remove the leading whitespace. If you want to keep it, delete the call.
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • you can use a char array because you "know" the length of the final string. – prof1990 Jan 10 '17 at 08:09
  • @prof1990 Yes this can be done with two char arrays, but not one. That's why I said "can't use _a_ char array". I just like to use StringBuilder more personally. – Sweeper Jan 10 '17 at 08:11
  • to each is own, but might I suggest you clarify your answer to saying it cant be done using just one char array? – prof1990 Jan 10 '17 at 08:14
  • Thanks for your contribution @Sweeper. – rrz0 Jan 10 '17 at 08:14
2

The following code should do as you ask. I think the code is pretty self explanatory., but feel free to ask if anything is unclear as to the why or how of the code.

// char[] partialWord  is used from question code
char[] result = new char[(partialWord.Length * 2) - 1];
for(int i = 0; i < result.Length; i++)
{
    result[i] = i % 2 == 0 ? partialWord[i / 2] : ' ';
}
return new string(result);
prof1990
  • 480
  • 3
  • 11
  • can you please comment on the line `result[i] = i % 2 == 0 ? partialWord[i / 2] : ' ';` . I am not fully understanding the meaning of `result[i] = i % 2 == 0 ?` – rrz0 Jan 10 '17 at 09:22
  • when I implemented this, this code is altering my original code in a way I am not managing to figure and all of my words are being left with only one unknown letter, whereas before the unknown letters or '_' was `int numberOfCharsToHide = word.Length / 2; ` – rrz0 Jan 10 '17 at 09:30
  • i % 2 == 0 ? partialWord[i / 2] : ' '; is a shorthand if. it is equivalent to if(i%2 == 0) {result[i] = partialWord[i/2];}else {result[i] = ' ';} – prof1990 Jan 10 '17 at 10:03
  • where did you put the code? it should replace your return new string(partialWord); – prof1990 Jan 10 '17 at 10:05
  • I am inserting the for loop under `partialWord[rIndex] = '_';` and I think that is the problem. – rrz0 Jan 10 '17 at 10:34
  • it needs to be outside the masking loop, so that is indeed likely to be the problem. – prof1990 Jan 10 '17 at 10:53