hi,string combination again

  • Thread starter Thread starter AsuWoo
  • Start date Start date
A

AsuWoo

hi,
I want to implement a function that prints all possible combinations of a
characters in a string,eg. input "123"into a textbox,
add "1","2","3","12","13","23","123",to a listbox,Or "ab" into a textbox,add
"a","b","ab"in a listbox,
but two combinations that differ only in ordering of their characters are
the same combination. in other words "12" is the same as "21",
i want to know hao to implement by c#,
thanx
 
this little app should do the trick.....i reckon it could be optimized, but
it works....

private void button1_Click(object sender, System.EventArgs e)
{

string word = "";

string anagram = textBox1.Text.ToString();

int size = 0;

int leng = anagram.Length;

listBox1.Items.Clear();

for (int i = 1; i <= leng; i++)

{

word = "";

size = i;

for (int j = 0 ; j < leng; j++)

{

word ="";

word += anagram[j];


if (word.Length == size)

{

listBox1.Items.Add(word);

word = "";

}

else

{

recurr_search_next_letter(word, size,j, leng, anagram);

}


}




}

}

private void recurr_search_next_letter(string word, int size, int teller,
int lengt, string anagram)

{

teller ++;

for (int j = teller; j < lengt ; j++)

{

word += anagram[j];


if (word.Length == size)

{

listBox1.Items.Add(word);

word = word.Substring(0, size-1);

}

else

{

recurr_search_next_letter(word, size,j, lengt, anagram);

word = word.Substring(0, word.Length-1);

}

}

word = "";

}





Hope this works for u,



Bart.
 
Back
Top