Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of all 4 letter words in the English language in C#

Tags:

c#

I am a beginner and have been playing around with C#. I have recently written the code for the very popular 'Cows and Bulls' word game. Its working for 2 players i.e; One player thinks of a word and the other person has to guess it. I am giving only 10 chances for guessing.

I now want to make the game for a single player(Computer v Human). I was wondering if there was anyway of getting all the 4 letter words in the English Language without Letter repetition(I have limited the game to 4 Letter words only). I know I can use an Enumeration and write down all the 4 letter words. But that's Hard Coding the Words. That would be my last option.I could type all the words in,but I would then have some idea of the word if I play the game.

like image 867
Shanks Avatar asked Nov 30 '25 12:11

Shanks


1 Answers

Assuming you have a list of words called words list

You can use regex to select 4 letter unique words

 List<string> 4letterUniqueWords=words
.Where(x=>Regex.IsMatch(x,"^(?=^[a-zA-Z]{4}$)(?!^.*?(.).*?\1.*?$).*?$"))
.Select(y=>y)
.ToList<string>();

^(?=^[a-zA-Z]{4}$)(?!^.*?(.).*?\1.*?$).*?$
  ---------------  ------------------ ---
        |                  |           |->matches if both the lookaheads are true
        |                  |->checks if there are any repeating words
        |->checks if the words contains 4 letter alphabetic words
like image 193
Anirudha Avatar answered Dec 03 '25 01:12

Anirudha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!