Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:44:15+00:00 2026-05-26T08:44:15+00:00

I am writing a basic hangman game. I want to store a string Word

  • 0

I am writing a basic hangman game. I want to store a string Word as the word to be found by the user. My problem is that I want to display this word as underscores (like you do in a paper hangman) but keep it’s string value. A friend of mine said this could be done via Regex but researching it doesn’t seem to help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:44:16+00:00Added an answer on May 26, 2026 at 8:44 am

    As suggested by Farinha, but using HashSet

    Some importang points: we are using a HashSet because in the end the letters can have two states: discovered (present in the HS) or not discovered (not present). We initialize the HashSet passing as a parameter StringComparer.InvariantCultureIgnoreCase because we consider L and l to be the same thing. To use the StringComparer.InvariantCultureIgnoreCase we have to use an HashSet<string> (an HS of strings) instead of an HashSet<char> (an HS of chars). So when using the discovered.Contains() we have to convert the c char in a string with a ToString

    static string ConvertWord(string word, HashSet<string> discovered)
    {
        StringBuilder sb = new StringBuilder(word.Length);
    
        foreach (char c in word)
        {
            if (discovered.Contains(c.ToString()))
            {
                sb.Append(c);
            }
            else
            {
                sb.Append('_');
            }
        }
    
        return sb.ToString();
    }
    
    HashSet<string> discovered = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
    
    // The "secret" word
    string word = "Hello world";
    
    // How to add letters to the ones discovered
    discovered.Add("l");
    
    // The word ready to be shown
    string convertWord = ConvertWord(word, discovered);
    

    We could have done the ConvertWord in much less characters, but for today it’s enough 🙂

    Ok… I’ll give you an example, in C# 4.0:

    static string ConvertWord(string word, HashSet<string> discovered)
    {
        return string.Concat(word.Select(p => discovered.Contains(p.ToString()) ? p : '_'));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a basic program that copies a string from an existing text file
I am writing a basic game engine and have an abstract class that represents
I am writing a basic web app, this has some list elements that I
I am writing a basic word processing application and am trying to settle on
I'm writing a basic crawler that simply caches pages with PHP. All it does
Hey--I'm writing a basic Rails app that uses the digg API. I'm trying to
I'm writing a basic tic tac toe game in Java. In my Board class
I need help writing a basic IRC bot that just connects to a channel..
I'm writing a basic UNIX program that involves processes sending messages to each other.
I am writing a very basic website which involves the user to use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.