Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 02.ObjectAndClasses-Randomize Words
- You are given a list of words in one line. Randomize their order and print each word at a separate line.
- Examples
- Input Output Comments
- Welcome to SoftUni and have fun learning programming learning The order of the words in the output will be different after each
- program execution.
- Welcome
- SoftUni
- and
- fun
- programming
- have
- to
- Hints
- • Split the input string by (space) and create an array of words.
- • Create a random number generator – an object rnd of type Random.
- • In a for-loop exchange each number at positions 0, 1, … words.Length-1 by a number at random position. To generate a random number in range use rnd.Next(minValue, maxValue). Note that by definition minValue is inclusive, but maxValue is exclusive.
- • Print each word in the array on new line.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _02RandomizeWords
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random random = new Random();
- string[] input = Console.ReadLine().Split();
- for (int i = 0; i < input.Length - 1; i++)
- {
- int index = random.Next(0, input.Length);
- var temporary = input[index];
- input[index] = input[i];
- input[i] = temporary;
- }
- Console.WriteLine(string.Join(Environment.NewLine, input));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement