i want create wordlists different word length. src for...
length=1:
for(int a=97;a<=122;a++) string foo=string.valueof((char)a);
length=2:
for(int a=97;a<=122;a++) for(int b=97;b<=122;b++) string foo=string.valueof((char)a+""+(char)b);
any ideas how improve code independent of actual string length?
as tejs suggests, you'll want use recursion. can't write loops have dynamic depth, can recurse level want (at least until run out of stack space):
//pseudocode! void generate(dictionary, string, depth) { if(depth == 0) { dictionary.add(string); } else { for(char c = 'a'; c <= 'z'; ++c) { generate(dictionary, string + c, depth - 1); } } }
just start if off like:
dictionary = new dictionary; generate(dictionary, '', depth);
and you're off. if want include strings up to desired length, it's easy tweak - add string dictionary, recurse if current depth greater zero.
hope helps!
Comments
Post a Comment