I want to make a program that would un-jumble some words.
I need to try all possible combinations of the words that can be formed, and then check if it is contained in a String variable, named dict
.
My code is:
public class UnJumble
{
public static void main(String args[])
{
String dict = "cat, rat, mat dog, let, den, pen, tag, art,";
String t = "tra";
int l = t.length();
for(int i=0; i<l; i++)
{
char a=t.charAt(i);
t = t.replaceFirst(a+"","");
l--;
for(int j=0; j<l; j++)
{
char b = t.charAt(j);
t = t.replaceFirst(b+"","");
l--;
for(int k=0; k<l; k++)
{
char c = t.charAt(k);
if(dict.contains(""+a+b+c+","))
{
System.out.println("\'"+a+b+c+"\' found.");
break;
}
}
l++;
t = new StringBuilder(t).insert(j,b+"").toString();
}
t = new StringBuilder(t).insert(i,a+"").toString();
l++;
}
}
}
The variable t
contains the word to be un-jumbled.
With this code, the output is:
'rat' found.
'art' found.
I think that I would need as many for
loops as there as characters in the String t
.
But I want to make it able to un-jumble words of an unknown length. So how can I achieve this?
I have tried searching on the Internet, and on SO. I found some answers on SO that are written in other programming languages which I don't understand.