3

I have to input a 5 lettered word and then it should print out different 3 lettered combinations of it but it isn't working and I'm not sure why.

E.g. If I input "hello", all different combinations such as "leh", "lol", "hle" etc should be returned.

static void Main(string[] args)
{
    Console.Write("Enter a five-letter word: ");
    String x = Console.ReadLine();

    for (int i = 0; i < x.Length; i++)
    {
        char letter = x[i];
        Random ran = new Random();
        for (int z = 1; z < 4; z++)
        {
            char y = x[ran.Next(0, x.Length)];
            Console.Write(y);
        }
    }
    Console.WriteLine();
}
DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • 1
    Do you mean all possible 3-letter combinations? Or just a couple of them? – vesan May 05 '15 at 04:37
  • 1
    If you need **all** combinations, you should not use random. – Orifjon May 05 '15 at 04:38
  • You should not use random anyhow, as you can have the same letter repeat. – Eric J. May 05 '15 at 04:42
  • Why would you want to use random, if your input is always 5-lettered word. you could use permutation of indexes. e.g. 230, 243,021. If you want to do something advance, you could create a function that returns permutation of indexes 5P3 and then compare to return three lettered unique combination. – ANewGuyInTown May 05 '15 at 05:02

2 Answers2

1

We can divide problem statement into two parts.

  1. Extracting combination of three characters out of five.
  2. Combination of three characters that are being extracted.

For first part we would declare an array that has possible combination of three character out of five. With the extracted three character we will make different combinations.

Note: You can add the combination in arrIndexes if I missed any.

string word = "hello";
int [,] arrIndexes = new int[9,3] {{0,1,2}, {0,1,3}, {0,1,4}, {0,2,3}, {0,2,4}, {0,3,4}, {1,2,3}, {1,3,4}, {2,3,4}};
for(int i=0; i < 9; i++)    
{       
     string sub = "";
     for(int j=0; j<3; j++)
        sub += word[arrIndexes[i,j]];

        Console.Write("{0}{1}{2}",sub[0], sub[1], sub[2]);
        Console.Write("\t{0}{1}{2}",sub[2], sub[1], sub[0]);
        Console.Write("\t{0}{1}{2}",sub[1], sub[0],sub[2]);
        Console.Write("\t{0}{1}{2}",sub[0], sub[2], sub[1]);        
        Console.Write("\t{0}{1}{2}",sub[1], sub[2], sub[0]);        
        Console.WriteLine("\t{0}{1}{2}",sub[2], sub[0], sub[1]);                
}

As pointed by @Rawling we can further generalize it to get the indexes for the character for getting three character word.

string word = "hello";      
for(int i=0; i<word.Length-2; i++)
    for(int j=i+1; j< word.Length-1; j++)          
       for(int k=j+1; k < word.Length; k++)
       {
         string sub = string.Format("{0}{1}{2}",word[i], word[j], word[k]);
         Console.Write("{0}{1}{2}",sub[0], sub[1], sub[2]);
         Console.Write("\t{0}{1}{2}",sub[2], sub[1], sub[0]);
         Console.Write("\t{0}{1}{2}",sub[1], sub[0],sub[2]);
         Console.Write("\t{0}{1}{2}",sub[0], sub[2], sub[1]);       
         Console.Write("\t{0}{1}{2}",sub[1], sub[2], sub[0]);       
         Console.WriteLine("\t{0}{1}{2}",sub[2], sub[0], sub[1]);       
        }

Output

hel  leh  ehl  hle  elh  lhe
hel  leh  ehl  hle  elh  lhe
heo  oeh  eho  hoe  eoh  ohe
hll  llh  lhl  hll  llh  lhl
hlo  olh  lho  hol  loh  ohl
hlo  olh  lho  hol  loh  ohl
ell  lle  lel  ell  lle  lel
elo  ole  leo  eol  loe  oel
elo  ole  leo  eol  loe  oel
llo  oll  llo  lol  lol  oll

Edit

You can further generalize it for getting the combinations of three letter extracted word

string word = "hello";  
for(int i=0; i<word.Length-2; i++)
    for(int j=i+1; j< word.Length-1; j++)          
       for(int k=j+1; k < word.Length; k++)
        {
            string sub = string.Format("{0}{1}{2}",word[i], word[j], word[k]);              
            for(int l=0; l<3;l++)                       
              for(int m=0; m<3;m++)
                 for(int n=0; n<3;n++)
                 if(l != m && m != n && l!=n)
                    Console.Write("\t{0}{1}{2}",sub[l], sub[m], sub[n]);                        
             Console.WriteLine("");         
        }   

Output

  hel  hle  ehl  elh  lhe  leh
  hel  hle  ehl  elh  lhe  leh
  heo  hoe  eho  eoh  ohe  oeh
  hll  hll  lhl  llh  lhl  llh
  hlo  hol  lho  loh  ohl  olh
  hlo  hol  lho  loh  ohl  olh
  ell  ell  lel  lle  lel  lle
  elo  eol  leo  loe  oel  ole
  elo  eol  leo  loe  oel  ole
  llo  lol  llo  lol  oll  oll
Adil
  • 146,340
  • 25
  • 209
  • 204
  • If only there were some *automated* way of doing this that would ensure you didn't miss anything (e.g. one element of `arrIndexes` and two `Console.Write` lines) and would scale to different length strings. – Rawling May 05 '15 at 06:50
  • 1
    Hmmm, this does not look like an programmed _algorithm_ but just a spelled out fixed solution - not very useful to a programmer. – DrKoch May 05 '15 at 07:02
  • @DrKoch, I was re-factoring the code as already pointed by Rawling – Adil May 05 '15 at 07:26
  • Now that you coded my suggestion, it looks much better ;) – DrKoch May 05 '15 at 07:31
  • Now you just need to calculate the permutations so you don't miss 2/3 of them... – Rawling May 05 '15 at 07:34
  • Honestly, I did not read your suggestion/answer until I wrote this. I did not even see your answer at that time. Your comment made me improve my answer and after that I thought to see if you have any comments/answer for the question. – Adil May 05 '15 at 07:35
0

To get all three-letter words of a given word, you need three indexes for the three result chars:

int firstChar = 0;
int secondChar = 1;
int thirdChar = 2;

Then three nested loops for these three indexes.

First loop runs from 0 to last-2.

Second loop runs from firstChar+1 to last-1.

Third loop runs form secondChar+1 to last.

inside the inner loop you build your result word:

string resultCombination = word[firstChar] + word[secondChar] + word[thirdChar];

Then calculate all possible permutations of this combination, using this SO answers

Add some error checking:

word must have three or more characters

Decide what to do if word contains the same character twice. Probably you need a version of word with unique characters.

Community
  • 1
  • 1
DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • This wouldn't produce, for example, `hle`, because it only outputs characters in the same order in which they appear in the input. But the part about the `for` loops is good; better than manually working them out. – Rawling May 05 '15 at 06:51
  • @Rawling thanks, you are right. I modified my answer – DrKoch May 05 '15 at 07:02