How to create an undefined number of cycles

I'm trying to create a program that will cover all of the letters.

I want, for example, show the aaaa, then aaab aaaz, then aaba etc. zzzz.

The problem is this: how to allow the user to enter the number of letters?

Here is my code with three letters:

Dim abc() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", _ "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} Console.ReadLine() Dim indx As Integer = 0 For a = 0 To 25 For b = 0 To 25 For c = 0 To 25 Console.WriteLine("{0}{1}{2}", abc(a), abc(b), abc(c)) Next Next Next 
+4
source share
4 answers

Recursion - a tool that you are looking for here. You want to repeat a certain depth; this depth - a user input. In this example, I gave a depth of 3 (which means all permutations of three letters, as you described in your question). You can change the value to whatever you want, or better yet, read the input from the user.

 Dim abc() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} Sub Main() Dim depth As Integer depth = 3 IterateAlphabet("", depth) Console.ReadKey() End Sub Sub IterateAlphabet(ByVal currentLetters As String, ByVal currentDepth As Integer) For letter = 0 To 25 Dim newLetters As String newLetters = currentLetters + abc(letter) If (currentDepth = 1) Then Console.WriteLine("{0}", newLetters) Else IterateAlphabet(newLetters, currentDepth - 1) End If Next End Sub "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k Dim abc() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} Sub Main() Dim depth As Integer depth = 3 IterateAlphabet("", depth) Console.ReadKey() End Sub Sub IterateAlphabet(ByVal currentLetters As String, ByVal currentDepth As Integer) For letter = 0 To 25 Dim newLetters As String newLetters = currentLetters + abc(letter) If (currentDepth = 1) Then Console.WriteLine("{0}", newLetters) Else IterateAlphabet(newLetters, currentDepth - 1) End If Next End Sub "," n "," o "," p "," q "," r "," s "," t "," u "," v "," w ", Dim abc() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} Sub Main() Dim depth As Integer depth = 3 IterateAlphabet("", depth) Console.ReadKey() End Sub Sub IterateAlphabet(ByVal currentLetters As String, ByVal currentDepth As Integer) For letter = 0 To 25 Dim newLetters As String newLetters = currentLetters + abc(letter) If (currentDepth = 1) Then Console.WriteLine("{0}", newLetters) Else IterateAlphabet(newLetters, currentDepth - 1) End If Next End Sub 
+3
source

I would suggest recursion, I'm not confident in their skills VB, but here's a solution in C #

 static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; static void Main(string[] args) { Console.Writeline("Enter the amount of characters"); int count = Int32.Parse( Console.ReadLine() ); outputStrings("", count); Console.ReadLine(); } static public void outputStrings(String startString, int letterCount) { for (int i = 0; i < letters.Length; i++) { String temp = startString; temp += letters[i]; if (temp.Length == letterCount) { Console.WriteLine(temp); } else { outputStrings(temp, letterCount); } } } a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k" static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; static void Main(string[] args) { Console.Writeline("Enter the amount of characters"); int count = Int32.Parse( Console.ReadLine() ); outputStrings("", count); Console.ReadLine(); } static public void outputStrings(String startString, int letterCount) { for (int i = 0; i < letters.Length; i++) { String temp = startString; temp += letters[i]; if (temp.Length == letterCount) { Console.WriteLine(temp); } else { outputStrings(temp, letterCount); } } } , static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; static void Main(string[] args) { Console.Writeline("Enter the amount of characters"); int count = Int32.Parse( Console.ReadLine() ); outputStrings("", count); Console.ReadLine(); } static public void outputStrings(String startString, int letterCount) { for (int i = 0; i < letters.Length; i++) { String temp = startString; temp += letters[i]; if (temp.Length == letterCount) { Console.WriteLine(temp); } else { outputStrings(temp, letterCount); } } } "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; static void Main(string[] args) { Console.Writeline("Enter the amount of characters"); int count = Int32.Parse( Console.ReadLine() ); outputStrings("", count); Console.ReadLine(); } static public void outputStrings(String startString, int letterCount) { for (int i = 0; i < letters.Length; i++) { String temp = startString; temp += letters[i]; if (temp.Length == letterCount) { Console.WriteLine(temp); } else { outputStrings(temp, letterCount); } } } "); static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; static void Main(string[] args) { Console.Writeline("Enter the amount of characters"); int count = Int32.Parse( Console.ReadLine() ); outputStrings("", count); Console.ReadLine(); } static public void outputStrings(String startString, int letterCount) { for (int i = 0; i < letters.Length; i++) { String temp = startString; temp += letters[i]; if (temp.Length == letterCount) { Console.WriteLine(temp); } else { outputStrings(temp, letterCount); } } } ); static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; static void Main(string[] args) { Console.Writeline("Enter the amount of characters"); int count = Int32.Parse( Console.ReadLine() ); outputStrings("", count); Console.ReadLine(); } static public void outputStrings(String startString, int letterCount) { for (int i = 0; i < letters.Length; i++) { String temp = startString; temp += letters[i]; if (temp.Length == letterCount) { Console.WriteLine(temp); } else { outputStrings(temp, letterCount); } } } int letterCount) static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; static void Main(string[] args) { Console.Writeline("Enter the amount of characters"); int count = Int32.Parse( Console.ReadLine() ); outputStrings("", count); Console.ReadLine(); } static public void outputStrings(String startString, int letterCount) { for (int i = 0; i < letters.Length; i++) { String temp = startString; temp += letters[i]; if (temp.Length == letterCount) { Console.WriteLine(temp); } else { outputStrings(temp, letterCount); } } } 
+2
source

You can see how to use the input field. It allows the user to select the length. MSDN InputBox

 Dim Length as Object Message = "Hello" Title = "Hello" InputBox(Message, Title, Length) Console.WriteLine(abc(Length)) 

It's not in my head, but I believe that with some tweaking, he should do the trick

0
source

Sorry, but I'm not sure in VB. This piece of code in Python that does the trick. Perhaps you can consider this pseudo-code and put it on VB.

Anyone who can change this in VB, edit my post.

strlen - is the length of the output lines. a ** b is a degree b. a% b is a module b.

 strlen = 5 for i in range (26 ** strlen): output = '' for j in range (strlen): output = chr ( (i / (26 ** j) ) % 26 + 97) + output print output 

And do not overdo it with the output length. For example, the length 10, for example, gives lines 141,167,095,653,376.

0
source

All Articles