Generate all passwords from given character set

Here given code implementation process.

/*
    Java program
    Generate all passwords from given character set 
*/
public class Combinations
{
	public void findResult(char[] record, String output, int n, int size)
	{
		if (output.length() > 0)
		{
			// Display output
			System.out.println(output);
		}
		if (n == 0)
		{
			return;
		}
		for (int i = 0; i < size; ++i)
		{
			// Find result using recursion
			findResult(record, output + record[i], n - 1, size);
		}
	}
	public static void main(String[] args)
	{
		Combinations task = new Combinations();
		// Character element
		char[] record = {
			'A' , 'T' , 'M' , '@' , '$'
		};
		// Get number of element in record set
		int size = record.length;
		// Length of generated string
		int n = 3;
		task.findResult(record, "", n, size);
	}
}

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program
    Generate all passwords from given character set 
*/
class Combinations
{
	public: 
    void findResult(char record[], string output, int n, int size)
	{
		if (output.length() > 0)
		{
			// Display output
			cout << output << endl;
		}
		if (n == 0)
		{
			return;
		}
		for (int i = 0; i < size; ++i)
		{
			// Find result using recursion
			this->findResult(record, output  +  record[i], n - 1, size);
		}
	}
};
int main()
{
	Combinations *task = new Combinations();
	// Character element
	char record[] = {
		'A' , 'T' , 'M' , '@' , '$'
	};
	// Get number of element in record set
	int size = sizeof(record) / sizeof(record[0]);
	// Length of generated string
	int n = 3;
	task->findResult(record, "", n, size);
	return 0;
}

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
// Include namespace system
using System;
/*
    Csharp program
    Generate all passwords from given character set 
*/
public class Combinations
{
	public void findResult(char[] record, String output, int n, int size)
	{
		if (output.Length > 0)
		{
			// Display output
			Console.WriteLine(output);
		}
		if (n == 0)
		{
			return;
		}
		for (int i = 0; i < size; ++i)
		{
			// Find result using recursion
			this.findResult(record, output + record[i], n - 1, size);
		}
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		// Character element
		char[] record = {
			'A' , 'T' , 'M' , '@' , '$'
		};
		// Get number of element in record set
		int size = record.Length;
		// Length of generated string
		int n = 3;
		task.findResult(record, "", n, size);
	}
}

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
package main

import "fmt"
/*
    Go program
    Generate all passwords from given character set 
*/
type Combinations struct {}
func getCombinations() * Combinations {
	var me *Combinations = &Combinations {}
	return me
}
func(this Combinations) findResult(record[] byte, 
	output string, n int, size int) {
	if len(output) > 0 {
		// Display output
		fmt.Println(output)
	}
	if n == 0 {
		return
	}
	for i := 0 ; i < size ; i++ {
		// Find result using recursion
		this.findResult(record, output + string(record[i]), n - 1, size)
	}
}
func main() {
	var task * Combinations = getCombinations()
	// Character element
	var record = [] byte {
		'A',
		'T',
		'M',
		'@',
		'$',
	}
	// Get number of element in record set
	var size int = len(record)
	// Length of generated string
	var n int = 3
	task.findResult(record, "", n, size)
}

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
<?php
/*
    Php program
    Generate all passwords from given character set 
*/
class Combinations
{
	public	function findResult($record, $output, $n, $size)
	{
		if (strlen($output) > 0)
		{
			// Display output
			echo($output."\n");
		}
		if ($n == 0)
		{
			return;
		}
		for ($i = 0; $i < $size; ++$i)
		{
			// Find result using recursion
			$this->findResult($record, $output.strval($record[$i]), $n - 1, $size);
		}
	}
}

function main()
{
	$task = new Combinations();
	// Character element
	$record = array('A', 'T', 'M', '@', '$');
	// Get number of element in record set
	$size = count($record);
	// Length of generated string
	$n = 3;
	$task->findResult($record, "", $n, $size);
}
main();

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
/*
    Node JS program
    Generate all passwords from given character set 
*/
class Combinations
{
	findResult(record, output, n, size)
	{
		if (output.length > 0)
		{
			// Display output
			console.log(output);
		}
		if (n == 0)
		{
			return;
		}
		for (var i = 0; i < size; ++i)
		{
			// Find result using recursion
			this.findResult(record, output + record[i], n - 1, size);
		}
	}
}

function main()
{
	var task = new Combinations();
	// Character element
	var record = ['A', 'T', 'M', '@', '$'];
	// Get number of element in record set
	var size = record.length;
	// Length of generated string
	var n = 3;
	task.findResult(record, "", n, size);
}
main();

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
#    Python 3 program
#    Generate all passwords from given character set 
class Combinations :
	def findResult(self, record, output, n, size) :
		if (len(output) > 0) :
			#  Display output
			print(output)
		
		if (n == 0) :
			return
		
		i = 0
		while (i < size) :
			#  Find result using recursion
			self.findResult(record, output + str(record[i]), n - 1, size)
			i += 1
		
	

def main() :
	task = Combinations()
	#  Character element
	record = ['A', 'T', 'M', '@', '$']
	#  Get number of element in record set
	size = len(record)
	#  Length of generated string
	n = 3
	task.findResult(record, "", n, size)

if __name__ == "__main__": main()

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
#    Ruby program
#    Generate all passwords from given character set 
class Combinations 
	def findResult(record, output, n, size) 
		if (output.length > 0) 
			#  Display output
			print(output, "\n")
		end

		if (n == 0) 
			return
		end

		i = 0
		while (i < size) 
			#  Find result using recursion
			self.findResult(record, output + record[i].to_s, n - 1, size)
			i += 1
		end

	end

end

def main() 
	task = Combinations.new()
	#  Character element
	record = ['A', 'T', 'M', '@', '$']
	#  Get number of element in record set
	size = record.length
	#  Length of generated string
	n = 3
	task.findResult(record, "", n, size)
end

main()

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
import scala.collection.mutable._;
/*
    Scala program
    Generate all passwords from given character set 
*/
class Combinations()
{
	def findResult(record: Array[Char], 
      	output: String, n: Int, size: Int): Unit = {
		if (output.length() > 0)
		{
			// Display output
			println(output);
		}
		if (n == 0)
		{
			return;
		}
		var i: Int = 0;
		while (i < size)
		{
			// Find result using recursion
			findResult(record, 
                       output + record(i).toString(), n - 1, size);
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		// Character element
		var record: Array[Char] = Array('A', 'T', 'M', '@', '$');
		// Get number of element in record set
		var size: Int = record.length;
		// Length of generated string
		var n: Int = 3;
		task.findResult(record, "", n, size);
	}
}

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
import Foundation;
/*
    Swift 4 program
    Generate all passwords from given character set 
*/
class Combinations
{
	func findResult(_ record: [Character], 
  		_ output: String, _ n: Int, _ size: Int)
	{
		if (output.count > 0)
		{
			// Display output
			print(output);
		}
		if (n == 0)
		{
			return;
		}
		var i: Int = 0;
		while (i < size)
		{
			// Find result using recursion
			self.findResult(record, output + String(record[i]), n - 1, size);
			i += 1;
		}
	}
}
func main()
{
	let task: Combinations = Combinations();
	// Character element
	let record: [Character] = ["A", "T", "M", "@", "$"];
	// Get number of element in record set
	let size: Int = record.count;
	// Length of generated string
	let n: Int = 3;
	task.findResult(record, "", n, size);
}
main();

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$
/*
    Kotlin program
    Generate all passwords from given character set 
*/
class Combinations
{
	fun findResult(record: Array < Char > , 
                    output: String, 
                    n: Int, size: Int): Unit
	{
		if (output.length > 0)
		{
			// Display output
			println(output);
		}
		if (n == 0)
		{
			return;
		}
		var i: Int = 0;
		while (i < size)
		{
			// Find result using recursion
			this.findResult(record, output + record[i].toString(), n - 1, size);
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	// Character element
	val record: Array < Char > = arrayOf('A', 'T', 'M', '@', '$');
	// Get number of element in record set
	val size: Int = record.count();
	// Length of generated string
	val n: Int = 3;
	task.findResult(record, "", n, size);
}

Output

A
AA
AAA
AAT
AAM
AA@
AA$
AT
ATA
ATT
ATM
AT@
AT$
AM
AMA
AMT
AMM
AM@
AM$
A@
A@A
A@T
A@M
A@@
A@$
A$
A$A
A$T
A$M
A$@
A$$
T
TA
TAA
TAT
TAM
TA@
TA$
TT
TTA
TTT
TTM
TT@
TT$
TM
TMA
TMT
TMM
TM@
TM$
T@
T@A
T@T
T@M
T@@
T@$
T$
T$A
T$T
T$M
T$@
T$$
M
MA
MAA
MAT
MAM
MA@
MA$
MT
MTA
MTT
MTM
MT@
MT$
MM
MMA
MMT
MMM
MM@
MM$
M@
M@A
M@T
M@M
M@@
M@$
M$
M$A
M$T
M$M
M$@
M$$
@
@A
@AA
@AT
@AM
@A@
@A$
@T
@TA
@TT
@TM
@T@
@T$
@M
@MA
@MT
@MM
@M@
@M$
@@
@@A
@@T
@@M
@@@
@@$
@$
@$A
@$T
@$M
@$@
@$$
$
$A
$AA
$AT
$AM
$A@
$A$
$T
$TA
$TT
$TM
$T@
$T$
$M
$MA
$MT
$MM
$M@
$M$
$@
$@A
$@T
$@M
$@@
$@$
$$
$$A
$$T
$$M
$$@
$$$


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved