I want to create wordlists with different word length. my 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 to improve this code so it is independent of the actual string length?
As Tejs suggests, you’ll want to use recursion. You can’t write loops to have dynamic depth, but you can recurse to any level you want (at least until you run out of stack space):
Just start if off like:
and you’re off. If you want to include all strings up to the desired length, it’s an easy tweak – just always add the string to the dictionary, but only recurse if the current depth is greater than zero.
Hope this helps!