Generating non repeating strings in c#

Currently I am writing a console based program in .net Core 3.1. The goal of the program is to have the user input a string and then the console will attempt to guess it. Example:

What shall I guess: string Ok Guessing String...  Code Cracked :) Your code is: string 

Anyways while I was creating the program I ran into a problem. The randomly generated string I created was repeating quite frequently, and if the codes were longer the problem exponentially increased.

I used this for my random string generation

string passw = Console.Readline();  for (int i = 0; i > -1; i++)             {                 var chars = "abcdefghijklmnopqrstuvwxyz";                 var stringChars = new char[passw.Length];                  for (int a = 0; a < passw.Length; a++)                 {                     stringChars[a] = chars[rnd.Next(chars.Length)];                 }                  var finalstring = new String(stringChars);                 Console.WriteLine(finalstring);                  if (finalstring== passw)                 {                     Console.WriteLine("");                     Console.WriteLine("Password Guessed: " + finalstring);                     Console.WriteLine("It took a total of: " + i + " guesses!");                     Environment.Exit(0);                 }             } 

How can I accomplish the goal? I saw some things about lists and similar things but was wondering if there was a better way to get the result I am looking for. Thank you! I can clarify anything if needed!

Add Comment
1 Answer(s)

Here’s a word guessing demo, done "hollywood" style:

enter image description here

Final output:

enter image description here

Because you should always show your progress when "cracking" a password…and "crack" it by guessing a random sorted order of letters?

Produced by:

static void Main(string[] args) {     string guess;     char response;     int tries = 0;                 Random rnd = new Random();     var chars = "abcdefghijklmnopqrstuvwxyz".ToCharArray();      do     {         Console.Clear();         Console.Write("Enter a word with no characters repeated in it: ");         string wordToGuess = Console.ReadLine().ToLower();          var letters = wordToGuess.ToCharArray().Distinct();         if (letters.All(c => char.IsLetter(c)) && letters.Count() == wordToGuess.Length && wordToGuess.Length > 0)         {             Console.WriteLine("Trying to guess your word...");             int row = Console.CursorTop;             Console.CursorVisible = false;             tries = 0;             do             {                 tries++;                 var shuffled = (chars.OrderBy(c => rnd.Next())).ToArray();                 guess = new String(shuffled.Take(wordToGuess.Length).ToArray());                 Console.SetCursorPosition(0, row);                 Console.WriteLine("chars = " + new string(shuffled));                 Console.Write("Guess: {0}, Tries: {1}", guess, tries.ToString("N0"));             } while (guess != wordToGuess);             Console.WriteLine();             Console.WriteLine("It took me {0} tries to guess your word: {1}", tries.ToString("N0"), guess);         }         else         {             Console.WriteLine("Invalid word to guess!");         }                 Console.CursorVisible = true;         Console.Write("Play again? Y/N: ");         response = Console.ReadKey().KeyChar;         Console.WriteLine();     } while (response == 'y' || response == 'Y');                      Console.Write("Press Enter to Quit");     Console.ReadLine(); } 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.