Simple string and how to produce all possible combinations of numbers, letters and special characters
4 views (last 30 days)
Show older comments
sai charan bandi
on 18 Jun 2015
Commented: sai charan bandi
on 18 Jun 2015
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
0 Comments
Accepted Answer
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)
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!