Simple string and how to produce all possible combinations of numbers, letters and special characters

4 views (last 30 days)
Hi everyone, I am facing some problem in using strings.
I want to produce all possible combinations of numbers,letters and special charactes.
for eg. a12,b12,c12,d12... 1a2,1a3,1a4,..1a9..1a, and so on and I want to pass these strings to some function in iterative for loop
If try to use for loop the problem will be as follows:
x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,/<>?";:\|}]''{[=+-_)(*&^%$#@!~`';
for i=1:length(x)
for j=1:length(x)
Data='x(i)x(j)'
Data1=sprintf('%X',Data);
.....
.....
end
end
If i use for loop using 'x(i)x(j)' it will be considered as a single string. So for every loop it will produce same hex value!!
If I remove '' and only give x(i)x(j) it doesnt count as any data type and matlab will give error.
I want to use this Data1 hex value in another function , like this I want to increase the loop further to produce all possible strings with 3 places and 4 places and so on....
Please help me

Accepted Answer

Guillaume
Guillaume on 18 Jun 2015
Edited: Guillaume on 18 Jun 2015
This is basic string concatenation, achieved with [] (or horzcat, or strcat):
Data = [x(i), x(j)]
Or you could use sprintf:
Data = sprintf('%c%c', x(i), x(j))
However, using a loop for generating all combinations is a performance killer, particularly when you have a function, nchoosek, specially designed for that:
x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,/<>?";:\|}]''{[=+-_)(*&^%$#@!~`';
allcombs = nchoosek(x, 2)
allhexs = cellfun(@(c) sprintf('%X', c), num2cell(allcombs, 2), 'UniformOutput', false)
  1 Comment
sai charan bandi
sai charan bandi on 18 Jun 2015
Thank You so much. nchoosek is awesome option but I have to use for loops anyway because I am trying to break one system using brute force(checking for all possible combinations), so it doesn't matter i think!

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!