One word and print different 3-letter combinations

I need to type in a 5 letter word and then it should print out different 3 letter combinations, but it doesn't work and I'm not sure why.

eg. If I enter "hello", all other 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();
}

      

+3


source to share


2 answers


We can divide the problem into two parts.

  • Extracting a combination of three characters out of five.
  • A combination of three characters to be extracted.

For the first part, we will declare an array that has a possible combination of three characters out of five. With the three symbols extracted, we will make different combinations.

Note. You can add a combination to 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 @Rawling pointed out, we can further generalize it to get the indices for a character to get a three-digit 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 to get three-word combinations extracted from a 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

      

+1


source


To get all three letter words of a given word, you need three indices for the three characters of the result:

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

      

Then three nested loops for those three indices.

The first cycle runs from 0 to the last-2.

The second loop runs from firstChar + 1 to last-1.

The third loop starts secondChar + 1 for the latter.



inside the inner loop, you create your result word:

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

      

Then calculate all possible permutations of that combination using this SO answer

Add error checking:

Word

must contain three or more characters

Decide what to do if a word contains the same character twice. You probably want a unique character version of the word.

0


source







All Articles