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

조회 수: 1 (최근 30일)
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

채택된 답변

Guillaume
Guillaume 2015년 6월 18일
편집: Guillaume 2015년 6월 18일
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
sai charan bandi
sai charan bandi 2015년 6월 18일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by