-3

I'm making a hangman game in C#, and it needs to show the player the sentence, but with ALL characters as an "X", EXCEPT a space with a space. So if the sentence is "I'm a person" it would be "XXX X XXXXXX". I have found a way to do this when EVERY character replaces with "X", but the problem is that, spaces is also replaced with X.

The code:

string WrVS = String.Empty;
for (int i = 0; i < Words[randomVal].Length; i++)
{
    WrVS += "X";
}
RWordCensored.Text = WrVS;

(randomVal is the random number that selects a random sentence from a list), (RWordCensored is a richTextBox that should show the sentence in the format I said.

I haven't found an answer on Google to my question.

/Viktor

VisualGhost
  • 49
  • 1
  • 1
  • 7
  • "I'm a person" is three words with a space between each word. – Ross Bush Aug 06 '14 at 18:32
  • 7
    `I haven't found an answer on Google to my question.` Is this the problem of new generation? You don't have to find an answer that does **exactly** what you want. You should adopt them to your problem. – L.B Aug 06 '14 at 18:32
  • Oh sorry, I meant "sentence" and not "word". And by "I haven't found an answer on Google to my question" I didn't mean just that I didn't found an answer to just my question. – VisualGhost Aug 06 '14 at 18:35

5 Answers5

9

Try this

var res=new Regex("\\S").Replace("I am a person","X");
//res will be X XX X XXXXXX
malkam
  • 2,337
  • 1
  • 14
  • 17
  • yours i liked the best , works like a charm. no need for fancy linq or string builders.. – Alok Apr 03 '15 at 20:41
6

Could be done using REGEX (I suppose) , but here is LINQ option.

string str = "I'm a person";
string replacedString = new String(str.Select(r => r == ' ' ? ' ' : 'X').ToArray());

And you will get back replacedString = "XXX X XXXXXX"

Habib
  • 219,104
  • 29
  • 407
  • 436
2

You should probably check to see what the character is that you're replacing before replacing it.

string WrVS = String.Empty;
for (int i = 0; i < Words[randomVal].Length; i++)
{
    char ch = Words[randomVal][i];
    WrVS += ch == ' ' ? ' ' : 'X';
}
RWordCensored.Text = WrVS;

Alternatively, you could use LINQ:

RWordCensored.Text = new string(
    (from ch in Words[randomVal]
     select ch => ch == ' ' ? ' ' : 'X').ToArray());

(or use the other format, as shown in Habib's answer.)

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
1

Add this check

if(Words[randomVal][i] != " ") //If the character is NOT a whitespace
{
    WrVS += "X";
}
else WrVS += " "; //If the character IS a white space
Chris
  • 681
  • 1
  • 6
  • 16
0

To determine whether the char is space you could use Char.IsWhiteSpace method:

StringBuilder WrVS = new StringBuilder(Words.Length);

foreach(var ch in Words)
{
   ch = Char.IsWhiteSpace(ch) ? ch : 'X';
   wrVs.Append(ch)
}

RWordCensored.Text = WrVS.ToString();

Also, when you are processing the string in such a manner you should use StringBuilder class - String vs. StringBuilder

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • 1
    I doubt that this hangman-esque game requires the efficiency of a `StringBuilder`, but it's best to develop good habits. – Michael Aug 06 '14 at 18:41