w3resource

C#: Concatenate a list of variable parameters

C# Sharp String: Exercise-35 with Solution

Write a C# Sharp program to concatenate a list of variable parameters.

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections;

public class Example35
{
   public static void Main()
   {
      const int WORD_SIZE = 4; // Constant representing the word size.

      // Define some 4-letter words to be scrambled.
      string[] words = { "abcd", "efgh", "ijkl", "mnop" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE]; // Array to hold random numbers as keys.
      string[] letters = new string[WORD_SIZE]; // Array to hold individual letters.
      // Initialize the random number generator.
      Random rnd = new Random();

      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}

Sample Output:

abcd --> cadb                                                                                                 
                                                                                                              
efgh --> efhg                                                                                                 
                                                                                                              
ijkl --> iklj                                                                                                 
                                                                                                              
mnop --> nmop

Flowchart :

Flowchart: C# Sharp Exercises - Concatenate a list of variable parameters.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to concatenate three objects, objects with a variable and 3-element object array.
Next: Write a C# Sharp program to concatenate three strings and display the result.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/string/csharp-string-exercise-35.php